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?
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Use Decimal.TryParse to check whether the v1 is convertible to decimal or not.

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

if (Decimal.TryParse(v1.Replace(",",""), out number))
{
     c5 = number;
}
else
{
     c5 = default(decimal?);
}

Open in new window


https://msdn.microsoft.com/en-us/library/9zbda557%28v=vs.110%29.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
string v1 = "null"; is Not a null but a string with the word 'null'.  Here are examples:  https://msdn.microsoft.com/en-us/library/system.string.isnullorempty%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
0
Fernando SotoRetiredCommented:
Hi eghtebas;

I have modified your code to work with v1 having the value of the string "null" or the string being empty.

string v1 = "null"; 

decimal? c5 = !(v1.Trim().ToLower() == "null" || v1.Trim() == String.Empty)
              ? decimal.Parse(v1.Replace(",","")) 
              : default(decimal?);

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

it_saigeDeveloperCommented:
Agreed with Steve, in order to specify a null string value, you would use either:
//  This is a null string, notice the missing quotes
string v1 = null;
// Or, this is a null string using the default keyword
string v1 = default(string);

Open in new window


To directly answer your question, however, as Lokesh pointed out, you should use TryParse instead of Parse.

Consider the following:
using System;

namespace EE_Q28631630
{
	class Program
	{
		static void Main(string[] args)
		{
			string v1 = "1,234,567.890";
			decimal temp;
			decimal? c5 = default(decimal?);

			c5 = !string.IsNullOrEmpty(v1) ? decimal.TryParse(v1, out temp) ? temp : default(decimal?) : null;

			Console.WriteLine("{0}", c5 != null ? c5.ToString() : "Not a number");

			v1 = default(string);
			c5 = !string.IsNullOrEmpty(v1) ? decimal.TryParse(v1, out temp) ? temp : default(decimal?) : null;

			Console.WriteLine("{0}", c5 != null ? c5.ToString() : "Not a number");

			v1 = "The brown fox jumped over the fence";
			c5 = !string.IsNullOrEmpty(v1) ? decimal.TryParse(v1, out temp) ? temp : default(decimal?) : null;

			Console.WriteLine("{0}", c5 != null ? c5.ToString() : "Not a number");

			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.JPGI would caution, however, against using such compounded ternary operations.

-saige-
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28631786.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
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
@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
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Fernando SotoRetiredCommented:
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..
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
0
Dave BaldwinFixer of ProblemsCommented:
Then you need to identify them and convert them explicitly.  Like...

if(v1string == 'null') v1number = null;
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.