By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

convert string to float

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Chi Tang" <ch********@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl...
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
Nov 15 '05 #2

P: n/a
Or use the decimal type.

"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:yk*******************@twister.austin.rr.com.. .

"Chi Tang" <ch********@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.