Solved

C# String to Decimal

Posted on 2009-07-12
5
754 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
[X]
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon S3 Images with .Net 3 30
C# Linq - Join two objects into one 3 31
c# DateTime Format validation 4 68
I need help adding validation to my MVC.Net view 6 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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