Java:将double转换为String

时间:2022-09-10 22:30:51

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).

我有一个双倍,其价值是10,000,000.00(一千万)。我必须将它转换为String。当使用方法toString我得到字符串“1.0E7”这是正确的遵循规范。不幸的是我需要字符串“10,000,000.00”(或等效的取决于区域设置)。

How can achieve this?

怎么能实现这个?

4 个解决方案

#1


12  

Try either

试试

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

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

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

#2


5  

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

除格式化程序外,您可以考虑使用java.math.BigDecimal类来精确表示数字。

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.

需要完全准确性的计算(例如财务计算)最好使用BigDecimal执行。浮点数学更适用于工程,图形和其他数学计算,其中可以牺牲一些精度来提高速度。

#3


4  

  public static void main(String[] args) throws ParseException {
    double aDouble = 10000000.00;
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    String aString = Double.toString(aDouble);
    System.out.println(nf.parse(aString));
  }

#4


2  

Look into "DecimalFormat". It lets you format the output pretty much however you want!

查看“DecimalFormat”。它允许您根据需要格式化输出!

#1


12  

Try either

试试

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

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

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

#2


5  

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

除格式化程序外,您可以考虑使用java.math.BigDecimal类来精确表示数字。

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.

需要完全准确性的计算(例如财务计算)最好使用BigDecimal执行。浮点数学更适用于工程,图形和其他数学计算,其中可以牺牲一些精度来提高速度。

#3


4  

  public static void main(String[] args) throws ParseException {
    double aDouble = 10000000.00;
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    String aString = Double.toString(aDouble);
    System.out.println(nf.parse(aString));
  }

#4


2  

Look into "DecimalFormat". It lets you format the output pretty much however you want!

查看“DecimalFormat”。它允许您根据需要格式化输出!