Solved

how can I format credit card number

Posted on 2006-07-06
5
460 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 20 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

Independent Software Vendors: 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

Facebook has became the #1 social media platform. People share many funny videos there, yet you don't know how to download them? Now you can download Videos from Facebook in just 3 simple steps.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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