Simple question on String to double
-
I have some problem in this simple passage:
String ^string_num="9,99"; double d_num1=Double::Parse(string_num); double d_num2=System::Convert::ToDouble(string_num);
i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:
-
I have some problem in this simple passage:
String ^string_num="9,99"; double d_num1=Double::Parse(string_num); double d_num2=System::Convert::ToDouble(string_num);
i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:
Floating point (and by extension double) numbers in computers can only ever be approximations of their decimal value, as the number is stored in binary scientific notation, i.e. exponent and mantissa, rather than decimal numeric. If you want accuracy then you need to stick to integers. See here[^] for a full description.
MVP 2010 - are they mad?
-
I have some problem in this simple passage:
String ^string_num="9,99"; double d_num1=Double::Parse(string_num); double d_num2=System::Convert::ToDouble(string_num);
i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:
... or use the Decimal type. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Floating point (and by extension double) numbers in computers can only ever be approximations of their decimal value, as the number is stored in binary scientific notation, i.e. exponent and mantissa, rather than decimal numeric. If you want accuracy then you need to stick to integers. See here[^] for a full description.
MVP 2010 - are they mad?
ok this make sense:), what i'll do is truncate after 2 decimal unit when visualizing the data to video thank to both of you
-
ok this make sense:), what i'll do is truncate after 2 decimal unit when visualizing the data to video thank to both of you
barbetto80 wrote:
what i'll do is truncate after 2 decimal unit when visualizing the data
Before taking this decision, make sure this will give you the results you need. For example using float/double for financial data is not a good idea as the roundings may cause loss of accuracy. This may not seem important with an example such as you showed above but it gets worse if you use the data in calculations and you can end up with significant errors in your results.
MVP 2010 - are they mad?
-
I have some problem in this simple passage:
String ^string_num="9,99"; double d_num1=Double::Parse(string_num); double d_num2=System::Convert::ToDouble(string_num);
i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:
try as following: String ^str="9.99"; System::Single d=Single::Parse(str);
-
try as following: String ^str="9.99"; System::Single d=Single::Parse(str);
-
barbetto80 wrote:
what i'll do is truncate after 2 decimal unit when visualizing the data
Before taking this decision, make sure this will give you the results you need. For example using float/double for financial data is not a good idea as the roundings may cause loss of accuracy. This may not seem important with an example such as you showed above but it gets worse if you use the data in calculations and you can end up with significant errors in your results.
MVP 2010 - are they mad?
i know...but that is only a simple representation of a zoom factor application on an image....so i think i can show a truncated value because the user don't need the full value. But thanks for the advice