convert string to float

  • Thread starter Thread starter Chi Tang
  • Start date Start date
C

Chi Tang

Hi,

I try to convert a string to a float but it alway comes out with extra
value. For example, the string input is '12.6' but the output is
'12.6000003814697' The following is my code to do the convert:

float fBalance = (float)System.Convert.ToSingle("12.6");

Should I specify a format string for the convert? Thanks for any help,
CT
 
Chi Tang said:
Hi,

I try to convert a string to a float but it alway comes out with extra
value. For example, the string input is '12.6' but the output is
'12.6000003814697' The following is my code to do the convert:

float fBalance = (float)System.Convert.ToSingle("12.6");

If I recall correctly, this is just a basic problem with
the way floating point numbers are represented in computers
and there will always be some degree of inaccuracy in those
types of numbers. If you want specific values, you
might try rounding the result to the specific number
of decimal places you wish.

-c
 
Or use the decimal type.

Chad Myers said:
If I recall correctly, this is just a basic problem with
the way floating point numbers are represented in computers
and there will always be some degree of inaccuracy in those
types of numbers. If you want specific values, you
might try rounding the result to the specific number
of decimal places you wish.

-c
 
Back
Top