Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Converting a double to a String

I have a double whose value is 10,000,000.00 (ten millions). I have to convert it to a String. When using the method toString I am getting the String "1.0E7" which is correct following the specification. Unfortunately I need the String "10,000,000.00" (or the equivalent depending on the locale).

How can achieve this?

like image 951
Luixv Avatar asked Nov 28 '22 20:11

Luixv


2 Answers

Try either

NumberFormat http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html

DecimalFormat http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html

like image 52
Lance Rushing Avatar answered Dec 10 '22 03:12

Lance Rushing


In addition to the formatter, you might consider using the java.math.BigDecimal class to represent numbers precisely.

Calculations where complete accuracy is required, such as financial calculations, are best performed with BigDecimal. Floating point math is better for engineering, graphics, and other mathematical computations where some accuracy can be sacrificed for speed.

like image 33
erickson Avatar answered Dec 10 '22 03:12

erickson