Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*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.

Students Click Here


convert string to float

convert string to float

convert string to float

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


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

RE: convert string to float


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

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

  There is no direct convertion from double to float.
"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.

RE: convert string to float

Just to put my 2c worth...

you can use the following:


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,

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close