We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,248 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Java converting long into currency

Hello I am having trouble converting a long (cents) into currency format.

My Code:

l

long doublePayment = 1099; //Should equal $10.99 
DecimalFormat dFormat = new DecimalFormat(); 
String formattedString = dFormat.format(doublePayment); 
System.out.println(formattedString);

Output: 1,099

I also tried:

long doublePayment = 1099; 
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); 
String s = n.format(doublePayment); 
System.out.println(s);

Output $1,099.00

Since this is cents, the output should be 10.99 or $10.99.

Cant figure out what I am doing wrong. Thanks!!!

2
Contributors
1
Reply
1 Hour
Discussion Span
7 Months Ago
Last Updated
2
Views
mcddewitt
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Currency formats assume an input in dollars. To show cents you need a floating point value, not a long which is an integer type. Declare doublePayment as a double, and set it to 10.99

JamesCherrill
... trying to help
Moderator
8,522 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0663 seconds using 2.73MB