0

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
2
Views
6 Years
Discussion Span
Last Post by JamesCherrill
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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.