1. Member
Join Date
Aug 2010
Posts
2
Rep Power
0

## 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.

2. How are you displaying the number? The printf() method has formatting controls that will give you what you want.

3. 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).

4. Member
Join Date
Aug 2010
Posts
2
Rep Power
0
We are using "Alert" to popup the message.

5. Is that javascript you're working with? Or java?

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
5
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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•