28

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

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?

Community
  • 1
  • 1
Alan
  • 371
  • 2
  • 4
  • 6
  • 4
    Not the same. The linked dupe candidate discusses how to print a rounded number, not how to actually round the value stored in the variable. – sblom Aug 29 '10 at 18:35

7 Answers7

90

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.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
bluedevil2k
  • 9,366
  • 8
  • 43
  • 57
  • Thanks bluedevil for taking the time to reply. I have tried your suggestion and get errors saying 'BigDecimal cannot be resolved to a type'. I'm afraid I'm new to this and need the idiots guide! – Alan Aug 29 '10 at 19:25
  • 8
    import java.math.BigDecimal – bluedevil2k Aug 29 '10 at 21:27
10

Just use Math.round()

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

mkm= (double)(Math.round(mkm*100))/100;
Sasquatch
  • 271
  • 3
  • 7
8
double formattedNumber = Double.parseDouble(new DecimalFormat("#.##").format(unformattedNumber));

worked for me :)

Paul
  • 89
  • 1
  • 1
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

joy
  • 327
  • 4
  • 11
1

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

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Thanks Thorbjorn for the reply. In the distant past when I used to program in basic I would use the way you suggested (although it was int(x) not round). I cannot figure out how to do this in android java though. – Alan Aug 29 '10 at 19:29
0

Try:

float number mkm = (((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt)*1000f;
int newNum = (int) mkm;
mkm = newNum/1000f; // Will return 3 decimal places
travega
  • 8,284
  • 16
  • 63
  • 91
  • Hi travega, I appriciate you taking the time to help me with this. I have copied and pasted the code you supplied, but am getting the error - 'syntax error, insert";" to complete LocalVariableDeclarationStatement'. I have checked the three ';' in the above code are in place. – Alan Aug 29 '10 at 20:25
-2

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

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

    }
alchemist
  • 1,081
  • 12
  • 17