Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

cathiec (Programmer) (OP)
11 Mar 04 7:19
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?
obislavu (Programmer)
11 Mar 04 9:29


string stringVal = "-9.34";
            decimal decval = System.Convert.ToDecimal(stringVal);
            double doubleval = System.Convert.ToDouble(stringVal);
            float fval = System.Convert.ToSingle(stringVal);
MaryDia (Programmer)
11 Mar 04 9:39


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.
snowmaster (Instructor)
17 Mar 04 8:54
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,

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!

Back To Forum

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