# Roudning problems

Printable View

• 08-26-2010, 05:55 PM
Nicke
Roudning problems
Hi

I just started to develop in java at my company and ran into a rounding problem that i can't solve as i like.
Got this number, 69,1999999999998 i want to round so i get 69,20.
But the result end up 69.2. I want my number to keep the zero.

My code looks like this
.........

var number: int = 69,1999999999998

number = Math.round(100000 * number) / 100000;

//number = 69.2

Does anyone know what i should do?
FYI, i'm calculating amounts on articles and want to keep the correct decimal value for the currency denomination.
• 08-26-2010, 06:00 PM
Norm
How are you displaying the number? The printf() method has formatting controls that will give you what you want.
• 08-26-2010, 06:04 PM
Fubarable
You understand of course that computers don't represent floating point numbers with exact precision (with exceptions of course), that what you're seeing isn't an error but is inherent in any programming language that uses floating points. If accuracy is of paramount importance, such as when dealing with financial numbers, then don't use doubles but instead use BigDecimals. More often though the accuracy of double is adequast and what is more important is probably how you display the results (which is what Norm was getting at above).
• 08-26-2010, 06:07 PM
Nicke
We are using "Alert" to popup the message.
• 08-26-2010, 07:55 PM
Norm
Is that javascript you're working with? Or java?
What class/mehtod is Alert ?
• 08-27-2010, 04:13 AM
al_Marshy_1981
Quote:

Originally Posted by Nicke
Hi

I just started to develop in java at my company and ran into a rounding problem that i can't solve as i like.
Got this number, 69,1999999999998 i want to round so i get 69,20.
But the result end up 69.2. I want my number to keep the zero.

My code looks like this
.........

var number: int = 69,1999999999998

number = Math.round(100000 * number) / 100000;

//number = 69.2

Does anyone know what i should do?
FYI, i'm calculating amounts on articles and want to keep the correct decimal value for the currency denomination.

var number smells like javascript and your talk of "alert" confirms it, you got the wrong forum, java is not javascript, javascript got its name from netscape who decided to use the name java as a prefix to its script because at the time of development java was seriously hot and they decided to jump on the bandwagon causing silly confusion to the likes of you.