Java十进制格式格式错误
Java decimal format wrong formatting
代码是:
private static DecimalFormat amDF=new DecimalFormat("###,###,###,##0.00");
amDF.setDecimalFormatSymbols(dfs); <- this only sets decimal separator, it works fine
amDF.setMinimumFractionDigits(2);
amDF.setMaximumFractionDigits(2);
格式化前值为 210103.6,格式化后应为:210.103,60,而不是 210.103,59。
为什么我输了 0.01?
编辑 #1:数字是 class 浮点数
的实例
编辑#2:numberOfDecimals = 2
您看到了在 java 中使用浮点数时的精度限制。它的 32 位精度根本不足以满足您的使用需求。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.
您可以通过将号码类型更改为双精度来演示问题。在这种情况下,您的 DecimalFormat.format() 输出正确的值,因为 double 具有足够的精度来保存您正在使用的数字。
final DecimalFormat df = new DecimalFormat("###,###,###,##0.00");
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
System.out.println(df.format(210103.6f));
=> 210.103,59
final DecimalFormat df = new DecimalFormat("###,###,###,##0.00");
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
System.out.println(df.format(210103.6d));
=> 210.103,60
这是 Expressions tab
在 eclipse 中的有趣行为 我尝试在调试模式下 运行 简单代码:
public static void main( String[] args ) throws Exception
{
float number = 210103.59f;
System.out.println( number );
}
断点与 System.out.println
和 Expressions tab
一致,我看到的值与您完全相同,即 210103.6.
代码是:
private static DecimalFormat amDF=new DecimalFormat("###,###,###,##0.00");
amDF.setDecimalFormatSymbols(dfs); <- this only sets decimal separator, it works fine
amDF.setMinimumFractionDigits(2);
amDF.setMaximumFractionDigits(2);
格式化前值为 210103.6,格式化后应为:210.103,60,而不是 210.103,59。
为什么我输了 0.01?
编辑 #1:数字是 class 浮点数
的实例编辑#2:numberOfDecimals = 2
您看到了在 java 中使用浮点数时的精度限制。它的 32 位精度根本不足以满足您的使用需求。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.
您可以通过将号码类型更改为双精度来演示问题。在这种情况下,您的 DecimalFormat.format() 输出正确的值,因为 double 具有足够的精度来保存您正在使用的数字。
final DecimalFormat df = new DecimalFormat("###,###,###,##0.00");
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
System.out.println(df.format(210103.6f));
=> 210.103,59
final DecimalFormat df = new DecimalFormat("###,###,###,##0.00");
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
System.out.println(df.format(210103.6d));
=> 210.103,60
这是 Expressions tab
在 eclipse 中的有趣行为 我尝试在调试模式下 运行 简单代码:
public static void main( String[] args ) throws Exception
{
float number = 210103.59f;
System.out.println( number );
}
断点与 System.out.println
和 Expressions tab
一致,我看到的值与您完全相同,即 210103.6.