×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# convert string to float

## convert string to float

(OP)
a column in my db is called cost. the type of this column is float. i am using visual studio . NET. A user enters the cost into a textfield on the form. i use the code:

float fCost = Convert.ToInt64(TextBox9.Text);

this works fine when numbers are entered into TextBox9.Text but when i try to enter a cost with a decimal point eg 9.99 i get an error.

when i use

double fcost = Convert.ToDouble(TextBox9.Text);

the error says cannot convert from double to float.

any suggestions?

### RE: convert string to float

#### CODE

string stringVal = "-9.34";
decimal decval = System.Convert.ToDecimal(stringVal);
double doubleval = System.Convert.ToDouble(stringVal);
float fval = System.Convert.ToSingle(stringVal);
-obislavu-

### RE: convert string to float

Suggestion:

double fcost = Convert.ToDouble(TextBox9.Text);
float result = (float)fcost;

or
float fcost = (float)Convert.ToDouble(TextBox9.Text);

There is no direct convertion from double to float.
And,
"For a conversion from double to float, the double value is rounded to the nearest float value. If the double value is too small to represent as a float, the result becomes positive zero or negative zero. If the double value is too large to represent as a float, the result becomes positive infinity or negative infinity. If the double value is NaN, the result is also NaN.
"

Hope this help.
Mary

### RE: convert string to float

Just to put my 2c worth...

you can use the following:

#### CODE

string str = "2.354";
float flt = float.Parse(str);

The Parse method is handy because it exists for every built-in function and a few more objects besides.

The Parse method for the float type also has an overload which allows you to specify a System.Globalization.NumberStyle enumeration. Very handy for specifying how your string should be converted.

All the best,
Snowmaster.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!