Solved

C# String to Decimal

Posted on 2009-07-12
5
743 Views
Last Modified: 2013-12-17
Hi,

I'm getting an xml message with this value:
<Value>55.55<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!
0
Comment
Question by:Endelm
5 Comments
 
LVL 5

Accepted Solution

by:
iUsername earned 300 total points
ID: 24835127
string s = "55.55";
decimal d = decimal.Parse(s);

decimal d is 55.55
0
 
LVL 12

Assisted Solution

by:expertsoul
expertsoul earned 200 total points
ID: 24835488
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.
 }
 else{
        //if s is in different format.
 }

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

0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24836114
use InvariantCulture if your server has , as a decimal seperator

decimal.Parse("55.55", CultureInfo.InvariantCulture);
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 24837374
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.
0
 

Author Closing Comment

by:Endelm
ID: 31602601
Thanks guys! :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now