Cut Digits from 0.1000000000001
-
Hey, how can I cut needless decimal digits from a calculation result e.g. calculation with pow(10.0,-1.0) 10^-1 = 0.100000000001 but I need to have 0.1
You can't. This is due to the precision of the floating point representation. But why do you want to do such a thing ? I think the error due to precision is very neglectable here.
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
Hey, how can I cut needless decimal digits from a calculation result e.g. calculation with pow(10.0,-1.0) 10^-1 = 0.100000000001 but I need to have 0.1
You mean for display, or internally ? ((int)(x*10))/10.0 would give you one decimal place. Not sure if there's a more built in way to do it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You mean for display, or internally ? ((int)(x*10))/10.0 would give you one decimal place. Not sure if there's a more built in way to do it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
So just format the value using
sprintf(..., "%.1f", ...)
.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb