5

I want to parse a string to float. And when the string is null it will pass 0 (as a float valule).

I was doing the parse like this :

aeVehicle.MSRP = float.Parse((drInvetory["MSRP"] ?? "0").ToString());

Which gave errors :

ERROR MESSAGE : Input string was not in a correct format.
ERROR SOURCE :    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Single.Parse(String s, NumberStyles style, NumberFormatInfo info)
   at System.Single.Parse(String s)

Please suggest the best way to handle this situation.

0

6 Answers 6

11

If drInvetory["MSRP"] comes from a DataRow, the null coalescing operator will not evaluate to true when compared to DBNull, thus the float parse will fail. You can compare for DBNull.Value, or use float.TryParse().

From Original Code

aeVehicle.MSRP = float.Parse(drInvetory["MSRP"] == DBNull.Value ? "0" : drInvetory["MSRP"].ToString());

Personally, I would check for DBNull and then cast to a float (or unbox to exact type, then cast). This saves a comparatively expensive string parse.

Better

aeVehicle.MSRP = drInvetory["MSRP"] == DBNull.Value ? default( float ) : 
(float)drInvetory["MSRP"];

SQL-CLR type mapping: http://msdn.microsoft.com/en-us/library/bb386947.aspx
See also: Difference between decimal, float and double in .NET?

Sign up to request clarification or add additional context in comments.

6 Comments

Never knew this..Yes..it comes from a DataRow
@Tim Medora..i double checked this..you are right..you taught me a great thing today..Thanks
From question seems that drInvetory["MSRP"] returns string. If so this will throw a exception: (float)drInvetory["MSRP"]. Better to use Convert.ToSingle()
@Petar no, it returns object. object can be null-coalesced with string - the result is an object expression; then the existing code calls .ToString() for the Parse. With that information, we can't really make any presumptions about what drInvetory["MSRP"] actually is. If it is actually a boxed float, then I agree with the "Better". If it is actually a string that happens to have a numeric-looking value, then Parse.
"MSRP" field is "Money" type in sql server. I fetch this and stores in a DataTable.
|
0
float f = 0f;
string s = (drInvetory["MSRP"] ?? "0").ToString();
if (s != null) {
    f = float.Parse(s, ...
}

3 Comments

0.0 is a double; 0f would be better, or just 0.
Thanks - been in Java land today (no excuse, 0.0 is double in java too).
Why ToString() ? It is already a string. And s cannot be null, so if seems not necessary. If drInvetory["MSRP"] is not a string this will not compile I think.
0

you should use the the TryParse :

    float num = 0;
    float.TryParse(s, out num);

Comments

0

It might caused by unexpected comma in your string. Try to throw away the comma first.

Comments

0
float result = 0;
float.TryParse(drInvetory["MSRP"], out result);
aeVehicle.MSRP = result;

This way, if the parse fails, then it will always fall-back to 0.

Comments

0

another simple alternate is:

aeVehicle.MSRP = float.Parse("0" + drInvetory["MSRP"].ToString());

It will work only for positive numbers. otherwise use Microsoft.VisualBasic.Val function which is the most convenient one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.