Use the str() builtin, which:
Returns a string containing a nicely printable representation of an object.
E.g:
>>> import decimal
>>> dec = decimal.Decimal('10.0')
>>> str(dec)
'10.0'
Answer from Gareth Latty on Stack OverflowPython Decimal to String - Stack Overflow
c# - Converting Decimal to string with non-default format - Stack Overflow
.net - decimal to string conversion in C# - Stack Overflow
c# - Convert string to decimal, keeping fractions - Stack Overflow
Videos
Use the str() builtin, which:
Returns a string containing a nicely printable representation of an object.
E.g:
>>> import decimal
>>> dec = decimal.Decimal('10.0')
>>> str(dec)
'10.0'
Use the string format function:
>>> from decimal import Decimal
>>> d = Decimal("0.0000000000000123123")
>>> s = '{0:f}'.format(d)
>>> print(s)
0.0000000000000123123
If you just type cast the number to a string it won't work for exponents:
>>> str(d)
'1.23123E-14'
You can use the decimal.ToString override to specify a formatting.
decimal amount = 120.00m;
string str = amount.ToString("0.00");
This can also be used when using String.Format.
Console.WriteLine("{0:0.00}", amount);
In the case of your first rule, it cannot be done on one line.
decimal amount = 120.00m;
string str = amount.ToString("0.00").Replace(".00", String.Empty);
There are different overloads for decimal.ToString based on what formatting you want.
Example
decimal d = 5.00
Console.WriteLine(d.ToString("C")); // for currency
See below for other overloads... specifier is what you put into the ToString(specifier)
MSDN Documentation on Decimal.ToString
decimal value = 16325.62m; string specifier;
// Use standard numeric format specifiers.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: G: 16325.62
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: C: $16,325.62
specifier = "E04";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: E04: 1.6326E+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: F: 16325.62
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: N: 16,325.62
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
// Displays: P: 163.26 %
// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: 0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays: #,#.00#;(#,#.00#): (16,325.62)
You can use the decimal.ToString override to specify a formatting.
decimal amount = 120.5m;
string str = amount.ToString("0.00");
This can also be used when using String.Format.
Console.WriteLine("{0:0.00}", amount);
In the case of your first and second rule, it cannot be done on one line.
decimal amount = 120.5m;
string str = amount.ToString("0.00").Replace(".00", String.Empty);
The following extension method should satisfy you requirements. The comments on the code was provided in OP and comments.
public static string ToFormattedString(this decimal d)
{
//The comma is not mandatory. but
var s = d.ToString();
var tokens = s.Split(new[]{"."}, StringSplitOptions.RemoveEmptyEntries);
//if there are no decimal points 12 then there should no be any zeros and periods (.)
if (tokens.Length == 1)
return s;
//I need to remove trailing zeros
var places = tokens[1].TrimEnd('0');
if (places.Length == 0)
return tokens[0];
//if there is only one decimal point ex- 0.5 then it should be displayed as 0.50
if (places.Length == 1)
return d.ToString("F2");
var format = string.Format("F{0}", places.Length);
return d.ToString(format);
}
Used like this
var x = new decimal[]{120.5m, 110, 25.356m};
foreach (var item in x)
Console.WriteLine("{0} => {1}", item.ToString(), item.ToFormattedString());
Output:
120.5 => 120.50
110 => 110
25.356 => 25.356
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));
}
}
Hello i was have the same issue, but it is easly, just do this:
string cadena="96.23";
decimal NoDecimal=decimal.parse(cadena.replace(".",","))
I think this is beacuse the notation that accept C# on decimal numbers are with a ","