?
Solved

C# String to Decimal

Posted on 2009-07-12
5
Medium Priority
?
759 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 1200 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 800 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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