Hi, I am calculating arithmetic mean of some $ values, so my numbers should have dollar part and cent part ( two decimal points )
double x = 3.46;
double y = 12. 13;
double arithmeticMean = ( x + y ) / 2; //7.795...should be 7.80
I don't care for stream output, I need to have correct values in memory ( in variable arithmeticMean ).
7.80....arithmeticMean / 2 = 3.9
7.795...arithmeticMean / 2 = 3.8975 //wrong
suljo2
0
Newbie Poster
Recommended Answers
Jump to PostYou will never get exact precision with floating point because of the way they are represented in memory.
Jump to Post3.8975 //wrong
If you round that to two decimals it will be "correct"(see Ancient Dragon)
Do all your floating calculations and start rounding afterwards.
When money is involved, the decimal type was invented for it.
All 7 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
ArkM
1,090
Postaholic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.