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

10-30-2002, 04:23 AM

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

11-07-2002, 11:03 AM

jayakumar Nallapalli

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

11-10-2002, 06:33 AM

Roly

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):

"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
>
>