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.

Similar Messages

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

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

  • Round off Double to 2 decimal places??

    Hi all the pro out there..
    anybody can tell me how to go about rounding off eg 5.9542 to 2 decimal places in java? i went to search in API Double class, no method that allow me to do that..
    is there any other way?
    pls advise thank you

    If the former, you can do it manually by multiplying by an appropriate number, casting to int, and then dividing by the same number.Although this won't always work.
    Suppose you want 1.125 --> 1.1
    1.125 --> 11.25 --> 11 --> 1.1 plus some epsilon, since 1.1 can't be represented exactly in base-2.Yes, I know that (I assume you knew I knew that). So, maybe the OP needs to use both methods in combination (and maybe use some rounding method [such as in the Math class] other than cast).

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

  • Rounding off double

    how do you round off a double to the nearest hundredth place?

    You can't because a double is typically not capable of representing an exact 100th place.
    Sylvia.

Maybe you are looking for

  • How to activate WBS Element in Sales Order

    Dear All, How to activate WBS element in sales order? Thanks & Regards, PM

  • I/o Error : Motherboard Changed

    Dear ALL, We are running the my SAP SCM - 5.0 ,   on IBM 346 Server , Windows 2003 OS from last few month during the Backup I/O error occurs. So hardware vendor suggest the t change the Motherboard ( planner) , After changing the Motherboard , mySAP

  • Get the SQLs In between a date range

    Get the SQLs irrespective of sessions in the order of its execution from a specified schema with in a date time range

  • Replace square brackets in SQL queries against ODBC

    We use PowerPivot to access an ODBC provider. PowerPivot generates queries with square brackets, which is not ANSI compliant and incompatible with this ODBC provider. In Office 2010, we had a workaround by modifying this file: C:\Program Files\Micros

  • Error connection system R3

    Hi, Today the following error is being produced: When I start the portal, the test connection is ok, but after of some minutes the connection failed and the test is not correct. The connection is Dedicated Application Server for R/3 System. Thanks, M