带舍入值的大十进制转换
Big decimal Conversion with roundup value
我有一个双重价值如
Double doubleValue=0.0001;
试图打印这个给了我一个输出 1.0E-4
所以我尝试使用 BigDecimal 将其作为值。
BigDecimal.valueOf(doubleValue)
最终输出为“0.00010”。
任何人都可以让我知道如何将舍入值设为“0.0001”。(1 之后没有 0 的结尾)
您可以尝试类似下面的代码
Double doubleValue=0.0001;
DecimalFormat f = new DecimalFormat("##.0000");
String formattedValue = f.format(doubleValue);
BigDecimal bigDecimalValue = new BigDecimal(formattedValue);
bigDecimalValue.stripTrailingZeros();
希望对您有所帮助
您正在使用 println 打印吗?您应该使用 printf 来格式化您的输出。
Double d = 0.000100;
System.out.printf("%.4f",d);
下面会打印“0.0001”
希望对您有所帮助!
我有一个双重价值如
Double doubleValue=0.0001;
试图打印这个给了我一个输出 1.0E-4
所以我尝试使用 BigDecimal 将其作为值。
BigDecimal.valueOf(doubleValue)
最终输出为“0.00010”。
任何人都可以让我知道如何将舍入值设为“0.0001”。(1 之后没有 0 的结尾)
您可以尝试类似下面的代码
Double doubleValue=0.0001;
DecimalFormat f = new DecimalFormat("##.0000");
String formattedValue = f.format(doubleValue);
BigDecimal bigDecimalValue = new BigDecimal(formattedValue);
bigDecimalValue.stripTrailingZeros();
希望对您有所帮助
您正在使用 println 打印吗?您应该使用 printf 来格式化您的输出。
Double d = 0.000100;
System.out.printf("%.4f",d);
下面会打印“0.0001”
希望对您有所帮助!