如何在不损失Java精度的情况下将String转换为Double?

时间:2022-04-11 15:54:15

Tried as below

试过如下

String d=new String("12.00");
Double dble =new Double(d.valueOf(d));
System.out.println(dble);

Output: 12.0

输出:12.0

But i want to get 12.00 precision

但我希望得到12.00精度

please let me know correct way without using format() method in string class

请不要在字符串类中使用format()方法让我知道正确的方法

4 个解决方案

#1


7  

Use BigDecimal Instead of a double:

使用BigDecimal而不是double:

String d = "12.00"; // No need for `new String("12.00")` here
BigDecimal decimal = new BigDecimal(d);

This works because BigDecimal maintains a "precision," and the BigDecimal(String) constructor sets that from the number of digits to the right of the ., and uses it in toString. So if you just dump it out with System.out.println(decimal);, it prints out 12.00.

这是有效的,因为BigDecimal维护一个“精度”,BigDecimal(String)构造函数从。的右边位数设置,并在toString中使用它。因此,如果您只是使用System.out.println(decimal);将其转储出来,则会打印出12.00。

#2


8  

Your problem is not a loss of precision, but the output format of your number and its number of decimals. You can use DecimalFormat to solve your problem.

您的问题不是精度损失,而是数字的输出格式及其小数位数。您可以使用DecimalFormat来解决您的问题。

DecimalFormat formatter = new DecimalFormat("#0.00");
String d = new String("12.00");
Double dble = new Double(d.valueOf(d));
System.out.println(formatter.format(dble));

I will also add that you can use DecimalFormatSymbols to choose which decimal separator to use. For example, a point :

我还要补充一点,你可以使用DecimalFormatSymbols来选择使用哪个小数分隔符。例如,一点:

DecimalFormatSymbols separator = new DecimalFormatSymbols();
separator.setDecimalSeparator('.');

Then, while declaring your DecimalFormat :

然后,在声明DecimalFormat的同时:

DecimalFormat formatter = new DecimalFormat("#0.00", separator);

#3


2  

You have not lost any precision, 12.0 is exactly equal to 12.00. If you want to display or print it with 2 decimal places, use java.text.DecimalFormat

你没有失去任何精度,12.0正好等于12.00。如果要显示或打印2位小数,请使用java.text.DecimalFormat

#4


1  

If you want to format output, use PrintStream#format(...):

如果要格式化输出,请使用PrintStream #format(...):

System.out.format("%.2f%n", dble);

There %.2f - two places after decimal point and %n - newline character.

有%.2f - 小数点后的两个位置和%n - 换行符。

UPDATE:

If you don't want to use PrintStream#format(...), use DecimalFormat#format(...).

如果您不想使用PrintStream #format(...),请使用DecimalFormat #format(...)。

#1


7  

Use BigDecimal Instead of a double:

使用BigDecimal而不是double:

String d = "12.00"; // No need for `new String("12.00")` here
BigDecimal decimal = new BigDecimal(d);

This works because BigDecimal maintains a "precision," and the BigDecimal(String) constructor sets that from the number of digits to the right of the ., and uses it in toString. So if you just dump it out with System.out.println(decimal);, it prints out 12.00.

这是有效的,因为BigDecimal维护一个“精度”,BigDecimal(String)构造函数从。的右边位数设置,并在toString中使用它。因此,如果您只是使用System.out.println(decimal);将其转储出来,则会打印出12.00。

#2


8  

Your problem is not a loss of precision, but the output format of your number and its number of decimals. You can use DecimalFormat to solve your problem.

您的问题不是精度损失,而是数字的输出格式及其小数位数。您可以使用DecimalFormat来解决您的问题。

DecimalFormat formatter = new DecimalFormat("#0.00");
String d = new String("12.00");
Double dble = new Double(d.valueOf(d));
System.out.println(formatter.format(dble));

I will also add that you can use DecimalFormatSymbols to choose which decimal separator to use. For example, a point :

我还要补充一点,你可以使用DecimalFormatSymbols来选择使用哪个小数分隔符。例如,一点:

DecimalFormatSymbols separator = new DecimalFormatSymbols();
separator.setDecimalSeparator('.');

Then, while declaring your DecimalFormat :

然后,在声明DecimalFormat的同时:

DecimalFormat formatter = new DecimalFormat("#0.00", separator);

#3


2  

You have not lost any precision, 12.0 is exactly equal to 12.00. If you want to display or print it with 2 decimal places, use java.text.DecimalFormat

你没有失去任何精度,12.0正好等于12.00。如果要显示或打印2位小数,请使用java.text.DecimalFormat

#4


1  

If you want to format output, use PrintStream#format(...):

如果要格式化输出,请使用PrintStream #format(...):

System.out.format("%.2f%n", dble);

There %.2f - two places after decimal point and %n - newline character.

有%.2f - 小数点后的两个位置和%n - 换行符。

UPDATE:

If you don't want to use PrintStream#format(...), use DecimalFormat#format(...).

如果您不想使用PrintStream #format(...),请使用DecimalFormat #format(...)。