Round a double to 2 descimal points

I have a double value and want to round it to 2 descimals.
eg. 123.454 to 123.45
How do i do this
Thanks

I have a double value and want to round it to 2
descimals.
eg. 123.454 to 123.45double d = 123.454;
double e = ( (double) Math.round(d*100))/100;

Similar Messages

  • Rounding a double

    Hi,
    What's the best (and fastest since I got lots of values) way to perform a rounding on a double value?
    I think what I'd like to do is half-up rounding, so when it should round up to 2 digits e.g., it should cut off the rest when the third position is a 0;1;2;3 or 4 and should add one to the second digit, when the third is one out of them: 5;6;7;8;9.
    I tried it myself this way. Now this has happened - at the end (converting int to double) it screws at some values - why and how can I fix that or what is a better way to do this, since this solution looks kind of goofy to me?
        public double roundValue( double dTemp, int iDigits){
             // splitting up into the pure int part of the NOT rounded value
             System.out.println("****   dTemp to round: " + dTemp);
             System.out.println("iDigits to round to: " + iDigits);
              int iIntPart = (int) dTemp;
              dTemp -= (double) iIntPart;
              System.out.println("iIntPart: " + iIntPart);
              System.out.println("dTemp - only minor digits: " + dTemp);
              // calculation of a factor for the lower digits
              double dDigits = 1.0;
              for(int cnt = 0; cnt < iDigits; ++cnt)
                  dDigits *= 10.0;
              System.out.println("dDigits: " + dDigits);
              System.out.println("dTemp * dDigits: " + (dTemp * dDigits));
              System.out.println("Math.round: " + Math.round(dTemp * dDigits));
              System.out.println("Math.round/dDigits: " + ((Math.round(dTemp * dDigits)) / dDigits));
              // processing Math.round()
              dTemp = (Math.round(dTemp * dDigits)) / dDigits;
              System.out.println("dTemp - after: " + dTemp);
              // dTemp is now rounded
              dTemp += (double) iIntPart;
              /* here sometimes, the value returns results like this - why?
               * 4.941519877284079 -> iDigits == 2 -> 4.9399999999999995
               * 5.94004582533272 -> iDigits == 2 -> 5.9399999999999995
              System.out.println("****   dTemp after adding the int part: " + dTemp);
              return dTemp;
        }

    Thanx I tried it out and I thought I had to use Math.round() the way I did. But still what happens at the conversion from an int to a double - just curious?
    I did it now this way - I thought Math.round has some probs, with the digits in front of the point:
        public double roundValue(double iValue, int iDecimalPlace) {
            double dPowerOfTen = 1;
            while (iDecimalPlace-- > 0){
               dPowerOfTen *= 10.0;
            return (Math.round(iValue * dPowerOfTen) / dPowerOfTen);
         }

  • Need some help in Rounding a double value to a whole number

    Hey Peeps,
    Need some help here, I got a method that returns a value in double after a series of calculation.
    I need to know how can I round the double value, so for example,
    1. if the value is 62222.22222222, it rounds to 62222 and
    2. if the value is 15555.555555, it rounds to 15556
    How can i do this
    Zub

    Hi Keerthi- Try this...
    1. if the value is 62222.22222222, it rounds to 62222 and
    double d = 62222.22222222;long l = (int)Math.round(d * 100); // truncatesd = l / 100.0;
    double d = 62222.22222222;
    System.out.println(d);
    long l = (int)Math.round(d * 100);
    // truncatesSystem.out.println(l);
    d = l / 100.0;System.out.println(d);
    for (int i = 0; i < 1000; i++)
    {    d -= 0.1;}
    for (int i = 0; i < 1000; i++)
    {    d += 0.1;}System.out.println(d);
    regards- Julie Bunavicz
    Output:
    62222.22222222
    62222
    62222.22
    62222.22000000000001

  • Rounding up doubles to 2 decimal places

    I'm currently rounding up a double in my program using Math.round(double)
    However I want to strip any trailing zeros from the output, so:
    1.235 would round to 1.24
    but 1.00 would round to 1
    How would I go about doing this?

    Also I'm making my own function to round the doubles,
    and they are returning doubles so I cannot return a
    string.If they are returning doubles that the number of "trailing zeros" is irrelevant, as that is formating. Really you should keep the precision until you want to display it, then use the formatter above.
    What are you doing?
    I'm going to take a shot in the dark; has this got something to do with money?

  • Round up double to 2 decimals

    How to round up double to 2 decimals? where in API i should look up? Thank you for your help!
    e.g., Book (including tax) :14.99 + 14.99 * 10% = 16.489 ---> 16.49

         * Scale decimal number via the rounding mode BigDecimal.ROUND_HALF_UP.
         * @param value Decimal value.
         * @param scale New scale.
         * @return Scaled number.
         * @since 1.8.3
        static public double getScaled(double value, int scale) {
            double result = value; //default: unscaled
            //use BigDecimal String constructor as this is the only exact way for double values
            result = new BigDecimal(""+value).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
            return result;
        }//getScaled()

  • Round a double value to a specific number of decimal places?

    Hello,
    Is there standard java function which will round a double value to a specified number of decimal places? Something like:
    double d = 4.34523;
    d = round(d, 2);
    where d is finally assigned the value of 4.34?
    Thanks!
    -exits

    No, because doubles hold values in binary (as do all values in a computer, of course, but there's no additional stuff to indicate decimal values).
    If you want values with specific rounding rules in decimal, use java.math.BigDecimal.
    If you just want to format the number with a specified number of decimal digits, use java.text.DecimalNumber.

  • What's the best way to round a double to 12 decimals?

    What's the best way to do it? The math functions don't seem quite right...

    I'm doing three different algoritms that are mathematically identical. I'm working with precisions from 2 to 12 significants digits (working with a function I got from a book), but the results are no exctly what I expect... For example, the one with 12 significant digits is 97.45322134230000001, or something like that... I'm making a method that receiving a double and an n makes a string with the correct number of significant digits... However, rounding the double beforehand would come in handy.

  • Rounding xs:double value

    Hi in bea xquery function pallette we only foung round() for decimal type.But we had a requirement to round xs:double values.
    How can i achive this ,any method for round double values or to convert double to decimal.

    does round( $someDouble ) not work?
    Can you please try a few things yourself before posting?
    If you already have, can you please post what you have tried?

  • Rounding a double value

    What is the easiest way to round a Double variable to 2 significant figures?
    Cheers Mike

    Oh, classical and very typical question on this forum.
    The short answer is nope. You don't do that and you don't need to do that. If you really really have to must doing that, you used the wrong type - double.
    By definition, double is an approximation. No gurantee on precision lose.
    --lichu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Rounding a double to a nearest 5000

    HI Guys,
    I wanted to know the easy way to rounding a double number to nearest to multiples of 5000.
    Thanks
    -nicedude

    Thank you . This is sure not a swing question at all, I should not have posted it here.
    Thanks you anyway for your answer. I did exactly the same thing. i thought somebody may come up with even a brighter idea, so i posted it.
    Thank you once again for your anwer.

  • May I know how to round a double number to 2 decimal points?

    if I have a double number 135.25689951591, how can I round this number to make it output as 135.26?

    double number = 135.25689951591;
    java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
    System.out.println(df.format(number));Alll this will do is display the value 135.26, number will still hold 135.25689951591

  • Rounding up doubles

    Just wondering if anyone knows how to round up a double so that for instance 100.232323 can be up to 100.
    This is due to trying to convert a price in pence to a meaningful price.
    Also is there anyway to represent a figure in pounds and pence???
    Regards.

    If you are working with money you shouldn't do floating point arithmetic. You should either use one of the following:
    1. java.math.BigDecimal. See example: double monthlyInterest = 5.5d / 1200d;
    double monthlyPayment = 700.25d;
    BigDecimal interestAmount = new BigDecimal(monthlyInterest).setScale(5,BigDecimal.ROUND_HALF_UP);
    BigDecimal principalAmount = new BigDecimal(monthlyPayment).setScale(2,BigDecimal.ROUND_HALF_UP);
    BigDecimal appliedPayment = payment.subtract(interestAmount).setScale(2,BigDecimal.ROUND_HALF_UP);
    //to get their double values just do (for example)
    double i = interestAmount.doubleValue();
    double p = principalAmount.doubleValue();
    double payment = appliedPayment.doubleValue();2. Multiply all numbers by a power of 10 (for example 100) and then truncate or round the decimals, then do your arithmetic, then divide by the same number. To truncate, just cast your double value to an int.int newValue = (int) someDoubleValue;To round, you can use any of the java.lang.Math methods.Math.ceil(someDoubleValue);
    Math.floor(someDoubleValue);
    Math.round(someDoubleValue);You can do your own research. :)
    The reason why you should not do floating point arithmetic is because it is not accurate.

  • Rounding of double numbers in J2ME

    Hi,
    I have a number like 25.1253345663. How do I round it to 25.1254 for a mobile application. Does J2ME have a library for this. Can I use J2SE libraries?
    Thanks

    Use this class
    If it is ok, say ok. :)
    public class Utils
    public static String formatDouble(double value, int decimals)
    String doubleStr = "" + value;
    int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".")
    : doubleStr.indexOf(",");
    // Decimal point can not be found...
    if (index == -1) return doubleStr;
    // Truncate all decimals
    if (decimals == 0)
    return doubleStr.substring(0, index);
    int len = index + decimals + 1;
    if (len >= doubleStr.length()) len = doubleStr.length();
    double d = Double.parseDouble(doubleStr.substring(0, len));
    return String.valueOf(d);
    }

  • Java rounding for Double data type

    Hi,
    Is there a way to change the default precision to 2 digits after decimal point for Double data type.

    java.text.DecimalFormat:
    import java.text.DecimalFormat;
    DecimalFormat format = new DecimalFormat("#.00");
    StringBuffer formatted = format.format(Math.PI, new StringBuffer(), new FieldPosition(0));
    System.out.println(formatted); // prints out 3.14Probably as applicable today as it was 3 years ago

  • Rounding a double to a format

    Hi,
    I'm trying to get a result for a formula in the format of ##.##. The result for one equation, for example, is 10.637129724208375 and I want it to be 10.64 (round to the nearest tenth, pretty much).
    Any suggestions?

    aeternaly wrote:
    I'm trying to get a result for a formula in the format of ##.##. The result for one equation, for example, is 10.637129724208375 and I want it to be 10.64 (round to the nearest tenth, pretty much).If it's just for display, look at DecimalFormat or String.format().
    If you actually need to store values to two decimal places, you're generally better off using BigDecimal.
    You can find the explanation why [url http://www.eason.com/library/math/floatingmath.pdf]here (the original) or [url http://floating-point-gui.de/]here (mybe a bit friendlier).
    Winston

Maybe you are looking for

  • PDF prints a thumbnail and backwards

    I recently upgraded a user to Acrobat Reader 9 and since then, when he tries to print a paper copy of PDF's to his post script printer, they come out as inverted and the size of a thumbnail picture.   Even if he sets the printer to the proper paper s

  • Quicktime 7.2 "Unexpectedly Quits" (crashes) - trying to capture

    There is something up with the quicktime software not communicating with the Firewire device. Quicktime 7.2 unexpectedly quits after I try to either: make a new recording, or goto preferences/recording Suggestions? Help! -Much appreciated. JSAP

  • Adhoc Query Outer Join

    Hi Is it possible to have adhoc queries with outer join since logical databases use inner joins.. Was just wondering if this is possible Thanks Case

  • Problem in Service Call

    Hi All, I'm trying to Use Service Call Functionality in Web Dynpro Component to use the class method for the context. When running the wizard I'm only getting the option of Function Module RADIO BUTTON ONLY. I'm not getting the Class method and other

  • Hierarchichal ALV using OO ABAP

    Hi, can you send sample code for Hierarchical ALV using OOP concepts. Regards Sandeep Reddy