Solved

how can I format credit card number

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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