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.

A Developer
  • 1,001
  • 3
  • 12
  • 32

6 Answers6

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?

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • Never knew this..Yes..it comes from a DataRow – A Developer Sep 20 '12 at 07:03
  • @Tim Medora..i double checked this..you are right..you taught me a great thing today..Thanks – A Developer Sep 20 '12 at 07:09
  • From question seems that drInvetory["MSRP"] returns string. If so this will throw a exception: (float)drInvetory["MSRP"]. Better to use Convert.ToSingle() – pero Sep 20 '12 at 07:12
  • 2
    @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`. – Marc Gravell Sep 20 '12 at 07:15
  • "MSRP" field is "Money" type in sql server. I fetch this and stores in a DataTable. – A Developer Sep 20 '12 at 07:21
  • In that case, you may want to use `decimal` (see the link I added to the answer). – Tim M. Sep 20 '12 at 07:24
0
float f = 0f;
string s = (drInvetory["MSRP"] ?? "0").ToString();
if (s != null) {
    f = float.Parse(s, ...
}
John3136
  • 28,809
  • 4
  • 51
  • 69
0

you should use the the TryParse :

    float num = 0;
    float.TryParse(s, out num);
meirrav
  • 761
  • 1
  • 9
  • 27
0

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

Niyoko
  • 7,512
  • 4
  • 32
  • 59
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.

CodeSpeaker
  • 810
  • 8
  • 22
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.