如何将double值舍入为2个小数点? [重复]

时间:2022-06-21 16:34:47

Possible Duplicate:
round double to two decimal places in java

可能重复:在java中将double加倍到两位小数

I want to round up the double value upto 2 decimal points.

我想将double值向上舍入到2个小数点。

for example: I have double d=2; and the result should be result =2.00

例如:我有双d = 2;结果应该是结果= 2.00

8 个解决方案

#1


29  

There's no difference in internal representation between 2 and 2.00. You can use Math.round to round a value to the nearest integer - to make that round to 2 decimal places you could multiply by 100, round, and then divide by 100, but you shouldn't expect the result to be exactly 2dps, due to the nature of binary floating point arithmetic.

内部表示在2和2.00之间没有区别。您可以使用Math.round将值舍入到最接近的整数 - 将该轮舍入到2位小数,您可以乘以100,舍入,然后除以100,但您不应期望结果正好是2dps,由于二进制浮点运算的性质。

If you're only interested in formatting a value to two decimal places, look at DecimalFormat - if you're interested in a number of decimal places while calculating you should really be using BigDecimal. That way you'll know that you really are dealing with decimal digits, rather than "the nearest available double value".

如果您只想将值格式化为两位小数,请查看DecimalFormat - 如果您在计算时对多个小数位感兴趣,那么您应该使用BigDecimal。这样你就会知道你真的在处理十进制数字,而不是“最接近的可用双精度值”。

Another option you may want to consider if you're always dealing with two decimal places is to store the value as a long or BigInteger, knowing that it's exactly 100 times the "real" value - effectively storing cents instead of dollars, for example.

如果你总是处理两个小数位,你可能想要考虑的另一个选择是将值存储为long或BigInteger,知道它正好是“真实”值的100倍 - 例如,有效地存储美分而不是美元。

#2


52  

Math.round(number*100.0)/100.0;

#3


31  

double RoundTo2Decimals(double val) {
            DecimalFormat df2 = new DecimalFormat("###.##");
        return Double.valueOf(df2.format(val));
}

#4


13  

import java.text.DecimalFormat;

public class RoundTest {
    public static void main(String[] args) {
        double i = 2;    
        DecimalFormat twoDForm = new DecimalFormat("#.00");
        System.out.println(twoDForm.format(i));
        double j=3.1;
        System.out.println(twoDForm.format(j));
        double k=4.144456;
        System.out.println(twoDForm.format(k));
    }
}

#5


11  

I guess that you need a formatted output.

我想你需要一个格式化的输出。

System.out.printf("%.2f",d);

#6


10  

you can also use this code

你也可以使用这段代码

public static double roundToDecimals(double d, int c)  
{   
   int temp = (int)(d * Math.pow(10 , c));  
   return ((double)temp)/Math.pow(10 , c);  
}

It gives you control of how many numbers after the point are needed.

它可以控制需要点数后的数量。

d = number to round;   
c = number of decimal places  

think it will be helpful

认为这会有所帮助

#7


5  

This would do it.

这样做会。

     public static void main(String[] args) {
        double d = 12.349678;
        int r = (int) Math.round(d*100);
        double f = r / 100.0;
       System.out.println(f);
     }

You can short this method, it's easy to understand that's why I have written like this.

你可以缩短这个方法,很容易理解这就是我写这样的原因。

#8


3  

public static double addDoubles(double a, double b) {
        BigDecimal A = new BigDecimal(a + "");
        BigDecimal B = new BigDecimal(b + "");
        return A.add(B).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

#1


29  

There's no difference in internal representation between 2 and 2.00. You can use Math.round to round a value to the nearest integer - to make that round to 2 decimal places you could multiply by 100, round, and then divide by 100, but you shouldn't expect the result to be exactly 2dps, due to the nature of binary floating point arithmetic.

内部表示在2和2.00之间没有区别。您可以使用Math.round将值舍入到最接近的整数 - 将该轮舍入到2位小数,您可以乘以100,舍入,然后除以100,但您不应期望结果正好是2dps,由于二进制浮点运算的性质。

If you're only interested in formatting a value to two decimal places, look at DecimalFormat - if you're interested in a number of decimal places while calculating you should really be using BigDecimal. That way you'll know that you really are dealing with decimal digits, rather than "the nearest available double value".

如果您只想将值格式化为两位小数,请查看DecimalFormat - 如果您在计算时对多个小数位感兴趣,那么您应该使用BigDecimal。这样你就会知道你真的在处理十进制数字,而不是“最接近的可用双精度值”。

Another option you may want to consider if you're always dealing with two decimal places is to store the value as a long or BigInteger, knowing that it's exactly 100 times the "real" value - effectively storing cents instead of dollars, for example.

如果你总是处理两个小数位,你可能想要考虑的另一个选择是将值存储为long或BigInteger,知道它正好是“真实”值的100倍 - 例如,有效地存储美分而不是美元。

#2


52  

Math.round(number*100.0)/100.0;

#3


31  

double RoundTo2Decimals(double val) {
            DecimalFormat df2 = new DecimalFormat("###.##");
        return Double.valueOf(df2.format(val));
}

#4


13  

import java.text.DecimalFormat;

public class RoundTest {
    public static void main(String[] args) {
        double i = 2;    
        DecimalFormat twoDForm = new DecimalFormat("#.00");
        System.out.println(twoDForm.format(i));
        double j=3.1;
        System.out.println(twoDForm.format(j));
        double k=4.144456;
        System.out.println(twoDForm.format(k));
    }
}

#5


11  

I guess that you need a formatted output.

我想你需要一个格式化的输出。

System.out.printf("%.2f",d);

#6


10  

you can also use this code

你也可以使用这段代码

public static double roundToDecimals(double d, int c)  
{   
   int temp = (int)(d * Math.pow(10 , c));  
   return ((double)temp)/Math.pow(10 , c);  
}

It gives you control of how many numbers after the point are needed.

它可以控制需要点数后的数量。

d = number to round;   
c = number of decimal places  

think it will be helpful

认为这会有所帮助

#7


5  

This would do it.

这样做会。

     public static void main(String[] args) {
        double d = 12.349678;
        int r = (int) Math.round(d*100);
        double f = r / 100.0;
       System.out.println(f);
     }

You can short this method, it's easy to understand that's why I have written like this.

你可以缩短这个方法,很容易理解这就是我写这样的原因。

#8


3  

public static double addDoubles(double a, double b) {
        BigDecimal A = new BigDecimal(a + "");
        BigDecimal B = new BigDecimal(b + "");
        return A.add(B).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }