舍入到小数点后2位[重复]

时间:2021-10-07 17:10:55

Possible Duplicate:
Round a double to 2 significant figures after decimal point

可能重复:将小数点后的两位有效数字舍入为2

I have:

mkm=((((amountdrug/fluidvol)*1000)/60)*infrate)/ptwt; 

in my Java code. The code works fine but returns to several decimal places. How do I limit it to just 2 or 3?

在我的Java代码中。代码工作正常,但返回到几个小数位。如何将其限制为2或3?

7 个解决方案

#1


89  

Don't use doubles. You can lose some precision. Here's a general purpose function.

不要使用双打。你可能会失去一些精确度。这是一个通用功能。

public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

You can call it with

你可以用它来打电话

round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);

"precision" being the number of decimal points you desire.

“precision”是你想要的小数点数。

#2


11  

Just use Math.round()

只需使用Math.round()

double mkm = ((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt;

mkm= (double)(Math.round(mkm*100))/100;

#3


8  

double formattedNumber = Double.parseDouble(new DecimalFormat("#.##").format(unformattedNumber));

worked for me :)

为我工作:)

#4


1  

Multiply by 1000, round, and divide back by 1000.

乘以1000,舍入,然后除以1000。

For basic Java: http://download.oracle.com/javase/tutorial/getStarted/index.html and http://download.oracle.com/javase/tutorial/java/index.html

对于基本Java:http://download.oracle.com/javase/tutorial/getStarted/index.html和http://download.oracle.com/javase/tutorial/java/index.html

#5


1  

BigDecimal a = new BigDecimal("12345.0789");
a = a.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_HALF_UP);
//Also check other rounding modes
System.out.println("a >> "+a.toPlainString()); //Returns 12345.08

BigDecimal a = new BigDecimal(“12345.0789”); a = a.divide(new BigDecimal(“1”),2,BigDecimal.ROUND_HALF_UP); //还要检查其他舍入模式System.out.println(“a >>”+ a.toPlainString()); //返回12345.08

#6


0  

Try:

float number mkm = (((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt)*1000f;
int newNum = (int) mkm;
mkm = newNum/1000f; // Will return 3 decimal places

#7


-2  

Create a class called Round and try using the method round as Round.round(targetValue, roundToDecimalPlaces) in your code

创建一个名为Round的类,并尝试在代码中使用方法round作为Round.round(targetValue,roundToDecimalPlaces)

public class Round {

        public static float round(float targetValue, int roundToDecimalPlaces ){

            int valueInTwoDecimalPlaces = (int) (targetValue * Math.pow(10, roundToDecimalPlaces));

            return (float) (valueInTwoDecimalPlaces / Math.pow(10, roundToDecimalPlaces));
        }

    }

#1


89  

Don't use doubles. You can lose some precision. Here's a general purpose function.

不要使用双打。你可能会失去一些精确度。这是一个通用功能。

public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

You can call it with

你可以用它来打电话

round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);

"precision" being the number of decimal points you desire.

“precision”是你想要的小数点数。

#2


11  

Just use Math.round()

只需使用Math.round()

double mkm = ((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt;

mkm= (double)(Math.round(mkm*100))/100;

#3


8  

double formattedNumber = Double.parseDouble(new DecimalFormat("#.##").format(unformattedNumber));

worked for me :)

为我工作:)

#4


1  

Multiply by 1000, round, and divide back by 1000.

乘以1000,舍入,然后除以1000。

For basic Java: http://download.oracle.com/javase/tutorial/getStarted/index.html and http://download.oracle.com/javase/tutorial/java/index.html

对于基本Java:http://download.oracle.com/javase/tutorial/getStarted/index.html和http://download.oracle.com/javase/tutorial/java/index.html

#5


1  

BigDecimal a = new BigDecimal("12345.0789");
a = a.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_HALF_UP);
//Also check other rounding modes
System.out.println("a >> "+a.toPlainString()); //Returns 12345.08

BigDecimal a = new BigDecimal(“12345.0789”); a = a.divide(new BigDecimal(“1”),2,BigDecimal.ROUND_HALF_UP); //还要检查其他舍入模式System.out.println(“a >>”+ a.toPlainString()); //返回12345.08

#6


0  

Try:

float number mkm = (((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt)*1000f;
int newNum = (int) mkm;
mkm = newNum/1000f; // Will return 3 decimal places

#7


-2  

Create a class called Round and try using the method round as Round.round(targetValue, roundToDecimalPlaces) in your code

创建一个名为Round的类,并尝试在代码中使用方法round作为Round.round(targetValue,roundToDecimalPlaces)

public class Round {

        public static float round(float targetValue, int roundToDecimalPlaces ){

            int valueInTwoDecimalPlaces = (int) (targetValue * Math.pow(10, roundToDecimalPlaces));

            return (float) (valueInTwoDecimalPlaces / Math.pow(10, roundToDecimalPlaces));
        }

    }