使用Math.round舍入到一个小数位?

时间:2022-04-12 16:57:21

I have these two variables

我有这两个变量

double num = 540.512
double sum = 1978.8

Then I did this expression

然后我做了这个表达

double total = Math.round((num/ sum * 100) * 10) / 10;

but I end up with 27.0.

但我最终以27.0结束。

In fact I have many other variables and when I use them in the expression I always get a 0 in the tenth's place.

事实上我还有很多其他变量,当我在表达式中使用它们时,我总是在第十个位置得到0。

9 个解决方案

#1


58  

Helpful method I created a while ago...

我刚才创建的有用方法......

private static double round (double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

#2


21  

try this

for example

DecimalFormat df = new DecimalFormat("#.##");
df.format(55.544545);

output:

55.54

#3


15  

The Math.round method returns a long (or an int if you pass in a float), and Java's integer division is the culprit. Cast it back to a double, or use a double literal when dividing by 10. Either:

Math.round方法返回一个long(如果你传入一个浮点数,则返回一个int),Java的整数除法是罪魁祸首。将其转换为双精度数,或者在除以10时使用双精简数。要么:

double total = (double) Math.round((num / sum * 100) * 10) / 10;

or

double total = Math.round((num / sum * 100) * 10) / 10.0;

Then you should get

那你应该得到

27.3

#4


13  

    Double toBeTruncated = new Double("2.25");

    Double truncatedDouble=new BigDecimal(toBeTruncated ).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

it will return 2.3

它将返回2.3

#5


8  

If you need this and similar operations more often, it may be more convenient to find the right library instead of implementing it yourself.

如果您需要更频繁地进行此类操作,那么找到合适的库而不是自己实现它可能会更方便。

Here are one-liners solving your question from Apache Commons Math using Precision, Colt using Functions, and Weka using Utils:

以下是使用Precision,使用函数的Colt和使用Utils的Weka从Apache Commons Math解决问题的单行:

double value = 540.512 / 1978.8 * 100;
// Apache commons math
double rounded1 = Precision.round(value, 1);
double rounded2 = Precision.round(value, 1, BigDecimal.ROUND_HALF_UP);
// Colt
double rounded3 = Functions.round(0.1).apply(value)
// Weka
double rounded4 = Utils.roundDouble(value, 1)

Maven dependencies:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.5</version>
</dependency>

<dependency>
    <groupId>colt</groupId>
    <artifactId>colt</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>nz.ac.waikato.cms.weka</groupId>
    <artifactId>weka-stable</artifactId>
    <version>3.6.12</version>
</dependency>

#6


4  

A neat alternative that is much more readable in my opinion, however, arguably a tad less efficient due to the conversions between double and String:

然而,在我看来,一个更加可读的简洁替代方案,由于double和String之间的转换,可能效率低一点:

double num = 540.512;
double sum = 1978.8;

// NOTE: This does take care of rounding
String str = String.format("%.1f", (num/sum) * 100.0); 

If you want the answer as a double, you could of course convert it back:

如果您希望将答案作为双精度,您当然可以将其转换回来:

double ans = Double.parseDouble(str);

#7


1  

Your method is right, all you have to do is add a .0 after both the tens and it will fix your problem!

你的方法是对的,所有你需要做的就是在几十之后添加一个.0它将解决你的问题!

double example = Math.round((187/35) * 10.0) / 10.0;

The output would be:

输出将是:

5.3

#8


1  

DecimalFormat decimalFormat = new DecimalFormat(".#");
String result = decimalFormat.format(12.763);                // -->  12.7

#9


0  

Double number= new Double("5.25");
Double tDouble=new BigDecimal(number).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

this will return it will return 5.3

这将返回它将返回5.3

#1


58  

Helpful method I created a while ago...

我刚才创建的有用方法......

private static double round (double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

#2


21  

try this

for example

DecimalFormat df = new DecimalFormat("#.##");
df.format(55.544545);

output:

55.54

#3


15  

The Math.round method returns a long (or an int if you pass in a float), and Java's integer division is the culprit. Cast it back to a double, or use a double literal when dividing by 10. Either:

Math.round方法返回一个long(如果你传入一个浮点数,则返回一个int),Java的整数除法是罪魁祸首。将其转换为双精度数,或者在除以10时使用双精简数。要么:

double total = (double) Math.round((num / sum * 100) * 10) / 10;

or

double total = Math.round((num / sum * 100) * 10) / 10.0;

Then you should get

那你应该得到

27.3

#4


13  

    Double toBeTruncated = new Double("2.25");

    Double truncatedDouble=new BigDecimal(toBeTruncated ).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

it will return 2.3

它将返回2.3

#5


8  

If you need this and similar operations more often, it may be more convenient to find the right library instead of implementing it yourself.

如果您需要更频繁地进行此类操作,那么找到合适的库而不是自己实现它可能会更方便。

Here are one-liners solving your question from Apache Commons Math using Precision, Colt using Functions, and Weka using Utils:

以下是使用Precision,使用函数的Colt和使用Utils的Weka从Apache Commons Math解决问题的单行:

double value = 540.512 / 1978.8 * 100;
// Apache commons math
double rounded1 = Precision.round(value, 1);
double rounded2 = Precision.round(value, 1, BigDecimal.ROUND_HALF_UP);
// Colt
double rounded3 = Functions.round(0.1).apply(value)
// Weka
double rounded4 = Utils.roundDouble(value, 1)

Maven dependencies:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.5</version>
</dependency>

<dependency>
    <groupId>colt</groupId>
    <artifactId>colt</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>nz.ac.waikato.cms.weka</groupId>
    <artifactId>weka-stable</artifactId>
    <version>3.6.12</version>
</dependency>

#6


4  

A neat alternative that is much more readable in my opinion, however, arguably a tad less efficient due to the conversions between double and String:

然而,在我看来,一个更加可读的简洁替代方案,由于double和String之间的转换,可能效率低一点:

double num = 540.512;
double sum = 1978.8;

// NOTE: This does take care of rounding
String str = String.format("%.1f", (num/sum) * 100.0); 

If you want the answer as a double, you could of course convert it back:

如果您希望将答案作为双精度,您当然可以将其转换回来:

double ans = Double.parseDouble(str);

#7


1  

Your method is right, all you have to do is add a .0 after both the tens and it will fix your problem!

你的方法是对的,所有你需要做的就是在几十之后添加一个.0它将解决你的问题!

double example = Math.round((187/35) * 10.0) / 10.0;

The output would be:

输出将是:

5.3

#8


1  

DecimalFormat decimalFormat = new DecimalFormat(".#");
String result = decimalFormat.format(12.763);                // -->  12.7

#9


0  

Double number= new Double("5.25");
Double tDouble=new BigDecimal(number).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

this will return it will return 5.3

这将返回它将返回5.3