Solved

how can I format credit card number

Posted on 2006-07-06
5
445 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
5 Comments
 
LVL 2

Expert Comment

by:vikrant4u2
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now