Click here to Skip to main content
15,900,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get a response Json object which i deserialize like

var v = JsonConvert.DeserializeObject<list><currentvalue>>(CurrentContent);

now the required value is in v[0].l_cur // "200.00"

string Value = v[0].l_cur.ToString().Trim();

I need to convert this to decimal Value

i tried

var CurrentStockValue = Convert.ToDecimal(Value);

var CurrentStockValue = Decimal.Parse(Value, NumberStyle.AllowDecimalPoint , CultureInfo.Invarient);

I get this error

"Input string was not in a correct format."

Can any one guide me through this..
Posted
Comments
Kornfeld Eliyahu Peter 8-Jun-15 4:34am    
It seems that after all Value has not numeric value (because Convert.ToDecimal("200.00") works just fine!)...
Hari-CodeBlogger 8-Jun-15 4:43am    
Thank you for your reply.
sorry. My Bad. string value comes like "Rs.200.00"
Thats was the problem
Milfje 8-Jun-15 4:39am    
Have you tried float.Parse() or decimal.Parse()?
Andy Lanng 8-Jun-15 4:40am    
I also double checked and both conversions work. Try value.Length as maybe there is a hidden character in there somewhere.
F-ES Sitecore 8-Jun-15 4:42am    
This could be a gloabalisation thing. If "." is not configured to be the decimal separator for your locale then you'll get these kinds of errors. If that's the case you'll need to force the locale to one that your string is a valid format for

Convert.ToDecimal(Value, System.Globalization.CultureInfo.GetCultureInfo("en-US"))

1 solution

Start by looking at your data: use the debugger to examine the content of Value immediately prior to the conversion attempt.
I'm guessing it doesn't contain "200.0" at all - but without knowing exactly what is in it, everybody is just guessing.
When you know what is in that, look at where is came from: v[0].l_cur, and see what that contains.

At some point it will be obvious that the data is invalid, and then it should be relatively simple to work out why. But until you know what the exact data and problem is, you can't fix anything!
 
Share this answer
 
Comments
Hari-CodeBlogger 8-Jun-15 4:44am    
Thank you for your reply.
sorry. My Bad. string value comes like "Rs.200.00"
Thats was the problem
OriginalGriff 8-Jun-15 4:51am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900