Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

c#, "null" to decimal null

I have

string v1 = "null";     //<-- the value of v1 is read from a site

decimal? c5 = !string.IsNullOrEmpty(v1)
                              ? decimal.Parse(v1.Replace(",",""))
                              : default(decimal?);

Error: Input string was not in a correct format.

Question: How can I handle this?
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Hi all,

Thank you very much for the good posts. After I got the response from Lokesh B R above, I started using it.

Apparently it works fine unless my new error has something to do with it. It is possible that my new question below it is related to the same issue:

https://www.experts-exchange.com/questions/28631786/c-error-String-or-binary-data-would-be-truncated.html

I will try the remainig three posts to see if it will take care of it.

If you have couple of minutes, please take a look at the link included in this post to comment on my new question.

Thanks,

Mike
@Dave,
string v1 = "null"; Yes, it is string read from a html file.

@Fernando,
You code is much shorter which is always good:
c5 = !string.IsNullOrEmpty(v1) ? decimal.Parse(v1.Replace(",", "")) : default(decimal?);
c6 = !string.IsNullOrEmpty(v2) ? decimal.Parse(v1.Replace(",", "")) : default(decimal?);

My new question at the link provided in my previous post, I guess, has to do with another issue.

@it_sage,

I am trying you post now.

Thanks,

Mike
string v1 = "null"; Yes, it is string read from a html file.
Ok... but "null", a string, does not equal null, the value.  That is shown on the Microsoft page I linked above.
Hi Mike;

To your Statement
You code is much shorter which is always good:
c5 = !string.IsNullOrEmpty(v1) ? decimal.Parse(v1.Replace(",", "")) : default(decimal?);
c6 = !string.IsNullOrEmpty(v2) ? decimal.Parse(v1.Replace(",", "")) : default(decimal?);
As you stated in this same post, "string v1 = "null"; Yes, it is string read from a html file.", which states that v1 is a string which can hold the word "null" and NOT a null value you need to handle that as a string because this statement will produce an error in above variables c5 when the value of the string is "null". This is because the statement, IsNullOrEmpty(v1) will not be true when "null" is held by the variable v1 because it is NOT a null value but a string..
Hi Fernando,

The information scrapped from this website are all string. In this particular situation v1 could be "0.001" or "null". I need to turn them to decimal (6,3) meaning 0.001 and null respectively.

Mike
Then you need to identify them and convert them explicitly.  Like...

if(v1string == 'null') v1number = null;
Hi Dave.

They are all string to begin with. Like:
"0.001" or "null"

The code has to identify them to figure out to use
decimal  or decimal?

as you know decimal? is a variable type as well.
As far as I know, decimal (6,3) is only found in SQL, not any version of C.  I guess you could approximate by using sprintf() with the correct formatting.  Something like...
vformatted = sprintf("%3.3d",v1number);

Open in new window