Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:18:30+00:00 2026-06-11T15:18:30+00:00

I want to parse a string to float. And when the string is null

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:18:32+00:00Added an answer on June 11, 2026 at 3:18 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm tring to parse a string to parse like this: Float.parseFloat(value_.toString()); when I parse
i want parse string like this 0.1142 0.0000 0.0000 0.0004 0.0000 0.0000 2299/2299 MakeRequest
I want to parse a string that represent a DateTime in UTC format. My
Let's say I want to parse a string with various opening and closing brackets
I want to parse a filter string similar to the following: ((Field1 = 'red')
I want to parse the input string and extract the values from it. My
I just want to parse the following string up until the END token then
I want to parse data-action in the string below. I've managed to get the
I want to parse last indian currency ie Rs from a string. For example:
I want to be able to parse a string to an object that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.