[英]What's Best Way of Formatting Decimal Numbers in Java?
I've been looking for formatting decimal numbers easily as in JavaScript by using toFixed()
.我一直在寻找像 JavaScript 一样使用
toFixed()
轻松格式化十进制数字。
What's your suggestion?你有什么建议?
Did you take a look at the BigDecimal class?? 您是否看过BigDecimal类? There are a number of
scale
related functions that might do what you need. 有许多与
scale
相关的功能可以满足您的需求。
Using String.format is likely to be the simplest. 使用String.format可能是最简单的。
var num = new Number(13.3714);
document.write(num.toFixed()+"<br />");
document.write(num.toFixed(1)+"<br />");
document.write(num.toFixed(3)+"<br />");
document.write(num.toFixed(10));
in Java 在Java中
double num = 13.3714;
// Uses String.format()
System.out.printf(
"%f<br />" +
"%.1f<br />" +
"%.3f<br />" +
"%.10f<br />", num, num, num, num);
将BigDecimal用于Java中与十进制相关的任何内容。
Best Way Depends Upon Requirement最佳方式取决于需求
specifies the format "%.3f" denotes 3 decimal places "%.4f" denotes 4 decimal
指定格式 "%.3f" 表示 3 个小数位 "%.4f" 表示 4 个小数位
for Example Double d = 32345.123214; System.out.format("%.2f", d);//output 32345.12
Double x = 4.0;
双倍 x = 4.0; System.out.println(String.format("%.5f", x));
System.out.println(String.format("%.5f", x)); //output4.00000
//输出4.00000
3. DecimalFormat DecimalFormat class has various format options to round up the decimal places based on the initialized format. 3. DecimalFormat DecimalFormat class 有多种格式选项,可以根据初始化格式对小数位进行四舍五入。 In the below example, we have initialized the DecimalFormat with the format as "#.##".
在下面的示例中,我们使用“#.##”的格式初始化了 DecimalFormat。 This means it limits the decimal place up to 2 digits
这意味着它将小数位限制为最多 2 位
Double d = 1234.67538911;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Using DecimalForamt: " + df.format(d));//output 1234.67
4.Using Math.round() 4.使用 Math.round()
The Math.round() method is another method to limit the decimal places in Java. Math.round() 方法是另一种限制 Java 中小数位数的方法。 If we want to round a number to 1 decimal place, then we multiply and divide the input number by 10.0 in the round() method.
如果我们想将一个数字四舍五入到小数点后 1 位,那么我们在 round() 方法中将输入数字除以 10.0。 Similarly, for 2 decimal places, we can use 100.0, for 3 decimal places, we can use 1000.0, and so on.
同样,对于 2 位小数,我们可以使用 100.0,对于 3 位小数,我们可以使用 1000.0,依此类推。
Double n = 123.4567;
System.out.println("Original value: " + n);
Double d = (double) (Math.round(n*10.0)/10.0);
System.out.println(d);
d = (double) (Math.round(n*100.0)/100.0);
System.out.println(d);
d = (double) (Math.round(n*1000.0)/1000.0);
System.out.println(d);
Original value: 123.4567
123.5
123.46
123.457
5.Using apache commons library apache-commons math3 library contains a Precision class method round() 5.使用apache commons library apache-commons math3 library包含一个Precision class方法round()
Double d = 456.98234;
Double n = Precision.round(d,2);
to add library添加库
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.