C# String to Decimal


I'm getting an xml message with this value:

I keep it in a string variable;

When I convert this to decimal, it gets converted to 5555 (not 55,55!)
decimal d = (decimal)str;

How can I convert this into 55,55 (not 5555) ?

Thank you guys!
Who is Participating?
iUsernameConnect With a Mentor Commented:
string s = "55.55";
decimal d = decimal.Parse(s);

decimal d is 55.55
expertsoulConnect With a Mentor Commented:
To convert a string to decimal you can use any of these methods:
string s = "55.55";

decimal d  = decimal.Parse(s); (as above, it works only with string)

decimal d;
 if(decimal.TryParse(s, out d)){
         //if s is a valid decimal string.
        //if s is in different format.

decimal d  = Convert.ToDecimal(s); (works with various datatypes)

use InvariantCulture if your server has , as a decimal seperator

decimal.Parse("55.55", CultureInfo.InvariantCulture);
Richard LeeSoftware EnthusiastCommented:
In your XML you show 55.55 (with a decimal point). At other places within your question your show 55,55 (with a comma). If its a comma in your XML then this is the reason it converts to 5555. Replace this with a decimal point.
EndelmAuthor Commented:
Thanks guys! :)
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.

All Courses

From novice to tech pro — start learning today.