Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to decimal, keeping fractions

Tags:

string

c#

decimal

I am trying to convert 1200.00 to decimal, but Decimal.Parse() removes .00. I've tried some different methods, but it always removes .00, except when I supply a fraction different than 0.

string value = "1200.00"; 

Method 1

 var convertDecimal = Decimal.Parse(value ,  NumberStyles.AllowThousands        | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol); 

Method 2

 var convertDecimal = Convert.ToDecimal(value); 

Method 3

var convertDecimal = Decimal.Parse(value,        NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture); 

How can I convert a string containing 1200.00 to a decimal containing 1200.00?

like image 847
NETQuestion Avatar asked Nov 24 '10 08:11

NETQuestion


People also ask

How do you convert string to decimal?

Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.

How do you convert string to decimal in Python?

If you are converting price (in string) to decimal price then.... from decimal import Decimal price = "14000,45" price_in_decimal = Decimal(price. replace(',','. '))

How do you convert a decimal to a long string?

You CAN convert any number to String with, for instance, Long. toString(long l) .


2 Answers

Hmm... I can't reproduce this:

using System;  class Test {     static void Main()             {         decimal d = decimal.Parse("1200.00");         Console.WriteLine(d); // Prints 1200.00     } } 

Are you sure it's not some other part of your code normalizing the decimal value later?

Just in case it's cultural issues, try this version which shouldn't depend on your locale at all:

using System; using System.Globalization;  class Test {     static void Main()             {         decimal d = decimal.Parse("1200.00", CultureInfo.InvariantCulture);         Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));     } } 
like image 165
Jon Skeet Avatar answered Oct 13 '22 11:10

Jon Skeet


I think your problem is when displaying the decimal, not the contents of it.

If you try

string value = "1200.00"; decimal d = decimal.Parse(s); string s = d.ToString(); 

s will contain the string "1200".

However if you change your code to this

string value = "1200.00"; decimal d = decimal.Parse(s); string s = d.ToString("0.00"); 

s will contain the string "1200.00" as you want it to do.

EDIT

Seems I'm braindead early in the morning today. I added the Parse statements now. However even my first code will output "1200.00", even if I expected it to output "1200". Seems like I'm learning something each day, and in this case obviously something that is quite basic.

So disregard this a an proper answer. We will probably need more code to identify your problem in this case.

like image 39
Øyvind Bråthen Avatar answered Oct 13 '22 12:10

Øyvind Bråthen