?
Solved

how can I format credit card number

Posted on 2006-07-06
5
Medium Priority
?
465 Views
Last Modified: 2010-04-01
I have a  BigDecimal number for credit card like
1111111111111111
I want to be displaied as xxxx-xxxx-xxxx-1111

thanks
0
Comment
Question by:salasadi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:vikrant4u2
ID: 17052401
hope the following piece of code might help.

DecimalFormat df = new DecimalFormat();
df.applyPattern("#,##,###,####");
BigDecimal bd = new BigDecimal("1111111111111111");
System.out.println("Result :"+df.format(bd.longValue()).replace(',','-'));;

here i have tried to use java.text.DecimalFormat class
here the replace(',','-') is used, since i was not able to use '-' as seperator in the pattern.

regards
vikrant
0
 

Author Comment

by:salasadi
ID: 17052540
Vikrant ...
Thanks for the example.  this displays the number as 11111-11111-11111-11111 I want ONLY the 4 digits displays and the rest will be xxxx-xxxx-xxxx-1111

0
 
LVL 4

Accepted Solution

by:
evnafets earned 80 total points
ID: 17055741
So the quick and dirty fix is to convert the BigDecimal into a string (using toString), grab the last four numbers, and put xxxx-xxxx-xxxx- in front of it.

BigDecimal bd = new BigDecimal("1234123412341234");
String cNum = bd.toString();
String filteredCnum = "xxxx-xxxx-xxxx-" + cNum.substring(cNum.length()-4);
        
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question