Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# String to Decimal

Posted on 2009-07-12
5
753 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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