473,508 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert string to float

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
2 64466

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
88991
by: Jason Chan | last post by:
i want to covert a querystring to float number in asp. what function should i use? i know cint() can convert string to int but how about float?
2
2723
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
2
13687
by: respect | last post by:
i want to convert real float number to 32-bit IEEE binary format in C++ ? plz help me in finding the codes as fast as u can ] Example for input -6.5, the output should be. 1 10000001...
3
3428
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a...
3
7969
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
9873
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the...
1
1874
by: tomerd11 | last post by:
please halp me to convert string to float
12
13508
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
8225
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
7
4198
by: Udara chathuranga | last post by:
How can I convert binary float number into a decimal float number ?
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.