Connecting Tech Pros Worldwide Help | Site Map

convert string to float

Chi Tang
Guest
 
Posts: n/a
#1: Nov 15 '05
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


Chad Myers
Guest
 
Posts: n/a
#2: Nov 15 '05

re: convert string to float



"Chi Tang" <chikuotang@hotmail.com> wrote in message
news:Ozv4GrpZDHA.2648@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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");[/color]

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


Michael Mayer
Guest
 
Posts: n/a
#3: Nov 15 '05

re: convert string to float


Or use the decimal type.

"Chad Myers" <cmyers@N0.SP.4M.austin.rr.com> wrote in message
news:ykx0b.10993$YC.743373@twister.austin.rr.com.. .[color=blue]
>
> "Chi Tang" <chikuotang@hotmail.com> wrote in message
> news:Ozv4GrpZDHA.2648@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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");[/color]
>
> 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
>
>[/color]


Closed Thread