Rounding numbers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Rounding numbers

Hybrid View

  1. #1
    Big Swifty Guest

    Rounding numbers


    What is the best way to deal with currency values?

    What I mean is during the course of my program, numbers representing currency
    values are calculated and stored as floats...consequently the artithmetic
    produces numbers like 6.345234 etc.
    I want to round these numbers to two decimal places (up or down as appropraite)
    so I can write them back to the database in that form.

    The Math.round() methods produce whole numbers...is their another way to
    achieve this?

    Many thanks

    Chris



  2. #2
    Guest

    Re: Rounding numbers


    "Big Swifty" <chrisg@music-exchange.co.uk> wrote:
    >
    >What is the best way to deal with currency values?
    >
    >What I mean is during the course of my program, numbers representing currency
    >values are calculated and stored as floats...consequently the artithmetic
    >produces numbers like 6.345234 etc.
    >I want to round these numbers to two decimal places (up or down as appropraite)
    >so I can write them back to the database in that form.
    >
    >The Math.round() methods produce whole numbers...is their another way to
    >achieve this?
    >
    >Many thanks
    >
    >Chris
    >
    >


    Nothing like answering your own queries *S*

    The BigDecimal class appears to be the secret.

    Thanks

    Chris




  3. #3
    jayakumar Nallapalli Guest

    Re: Rounding numbers


    <java.@127.0.0.1> wrote:
    >
    >"Big Swifty" <chrisg@music-exchange.co.uk> wrote:
    >>
    >>What is the best way to deal with currency values?
    >>
    >>What I mean is during the course of my program, numbers representing currency
    >>values are calculated and stored as floats...consequently the artithmetic
    >>produces numbers like 6.345234 etc.
    >>I want to round these numbers to two decimal places (up or down as appropraite)
    >>so I can write them back to the database in that form.
    >>
    >>The Math.round() methods produce whole numbers...is their another way to
    >>achieve this?
    >>
    >>Many thanks
    >>
    >>Chris
    >>
    >>

    >
    >Nothing like answering your own queries *S*
    >
    >The BigDecimal class appears to be the secret.
    >
    >Thanks
    >
    >Chris
    >
    >
    >
    >Easy way to TO do that one is I think This will Solve Your Problem



    java.math.BigDecimal bg = new java.math.BigDecimal(yourDecimalNumber);
    double double Value =bg.setScale(2,bg.ROUND_HALF_EVEN).doubleValue();



  4. #4
    Roly Guest

    Re: Rounding numbers


    Hey Chris,
    You can still take advantage of the Math.round() method. Multiply the decimal
    number such as 6.345234 by 100 and send that result to round.
    So you'll get Math.round(634.5234) returning 635. Then just divide by 100.0
    and you get you're rounded value.

    Really it's just your decimal value times 10^n where n is the number of decimal
    places you want to round to, then divide by this same same number(and tack
    on a .0).

    *** Just be aware of the intermediate results. the Math.round() method will
    return an integer result. If you divide this integer result by 100, you will
    get an integer division, and ultimately an integer being assigned. ***


    For example:
    float roundedVal;
    float taxedGoods;

    taxedGoods = 4.2863; //calculated elsewhere


    The following will return 4.0 since 429 / 100 is integer division and will
    truncate the fractional. (The RHS returns 4, but since roundedVal is flost
    it's stored as 4.0):

    roundedVal = (Math.round(taxedGoods * 100) / 100);



    Tacking on the .0 in the divisor will yield floating point division keeping
    the fractional and giving the intended results:

    //assigns 4.29 to roundedVal
    roundedVal = (Math.round(taxedGoods * 100) / 100.0f);


    Hope this helps,
    Roly




    "Big Swifty" <chrisg@music-exchange.co.uk> wrote:
    >
    >What is the best way to deal with currency values?
    >
    >What I mean is during the course of my program, numbers representing currency
    >values are calculated and stored as floats...consequently the artithmetic
    >produces numbers like 6.345234 etc.
    >I want to round these numbers to two decimal places (up or down as appropraite)
    >so I can write them back to the database in that form.
    >
    >The Math.round() methods produce whole numbers...is their another way to
    >achieve this?
    >
    >Many thanks
    >
    >Chris
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center