简体   繁体   English

在 Java 中格式化小数的最佳方法是什么?

[英]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最佳方式取决于需求

  1. Using the format() method使用 format() 方法

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
  1. String.format字符串格式

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.

相关问题 格式化数字的最佳设计模式是什么? - What's the best design pattern for formatting numbers? 用Java从32位值的一部分中计算部分十进制值的最佳方法是什么 - What's the best way in Java to calculate a partial decimal value from parts of a 32-bit value 在Java中以美分(整数)转换美元(大十进制)的最佳方法是什么? - What is the best way to convert Dollars (Big Decimal) in Cents (Integer) in java? Java 中十进制数的正则表达式是什么? - What is the Regex for decimal numbers in Java? 为学习算法手动分离数字的最佳方法是什么 - What's the best way to separate numbers manually for learning algorithm 用十进制格式化数字 - Formatting Numbers with Decimal 最佳体系结构-收听Java中的原子供稿的最佳方法是什么? - Best Architecture - What's the best way to listen to an atom feed in java? 在Java中生成随机数的最佳种子数是什么? - What's the best seed number for generating random numbers in java? 格式化数字 - Java 中的小数位数字分组 - Formatting numbers - grouping of decimal-place digits in Java 检查字符是否是Java中的元音的最佳方法是什么? - What's the best way to check if a character is a vowel in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM