找到你要的答案

Q:Using Math.round to round to one decimal place?

Q:使用math.round绕到小数点后一位?

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.

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.

我有这两个变量

double num = 540.512
double sum = 1978.8

然后我做了这个表达

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

但我最终与27。

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

answer1: 回答1:

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

我创建的一个有用的方法…

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

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:

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

的math.round方法返回一个长(或int如果你通过在一个浮动),和java的整数除法是罪魁祸首。将其转换为双值,或在除以10时使用双文本。无论是:

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

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

然后你应该得到

27.3
answer3: 回答3:

try this

for example

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

output:

55.54

试试这个

f或 example

DecimalF或mat df = new DecimalF或mat("#.##");
df.f或mat(55.544545);

输出:

55.54
answer4: 回答4:
    Double toBeTruncated = new Double("2.25");

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

it will return 2.3

    Double toBeTruncated = new Double("2.25");

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

它将返回2.3

answer5: 回答5:

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:

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>

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

这是一套从Apache Commons数学使用精确的解决你的问题,Colt的使用功能,并使用工具Weka:

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的依赖:

<dependency>
    <groupId>或g.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>
answer6: 回答6:

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

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

double num = 540.512;
double sum = 1978.8;

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

如果你想把答案作为一个双重的,你当然可以转换回:

double ans = Double.parseDouble(str);
java  decimal  rounding