Java 使用 Math.round 四舍五入到小数点后一位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22186778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:11:42  来源:igfitidea点击:

Using Math.round to round to one decimal place?

javadecimalrounding

提问by user3344542

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.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。

回答by rgettman

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

Math.round方法返回 alongint如果传入 a 则返回an float),而 Java 的整数除法是罪魁祸首。将其转换回 a double,或double在除以 时使用文字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

回答by jpdymond

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;
}

回答by user3381763

try this

尝试这个

for example

例如

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

output:

输出:

55.54

回答by Sankar

    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

回答by Mifeet

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 使用Functions和 Weka 使用Utils从 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:

Maven 依赖项:

<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>

回答by Micah Stairs

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);

回答by Micah Stairs

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

回答by Dan Alboteanu

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

回答by sankar ganesh

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