19

I need to convert decimal to string, in this rulers:

120.00 - "120"
120.01 - "120.01"
120.50 - "120.50"
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1046047
  • 221
  • 1
  • 2
  • 4

4 Answers4

31

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);
Joshua
  • 8,112
  • 3
  • 35
  • 40
  • 1
    but if amount 120.10m => 120.1 and it's bad – user1046047 Nov 14 '11 at 17:27
  • I also wrote a commentary, no other options? – user1046047 Nov 14 '11 at 17:36
  • @user1046047 check out my answer. I specify the overloads that you can pick – Kellen Stuart Aug 23 '18 at 20:52
  • I get `cannot convert from double to string` when I try to use `decimal amount = 120.60m; string str = amount.ToString("0.00");` Found out I needed to use `amount.ToString("F6");`. In my case I needed 6 placevalues, but I don't think it was the number of decimal points that was the problem because it complained if I did `string str = amount.ToString("0.00");` or `string str = amount.ToString("0.000000");` I was using a `numeric` database field, which was stored using a `decimal` object. Ran across this https://stackoverflow.com/questions/533931/convert-double-to-string - which helped me. – vapcguy Sep 18 '18 at 22:35
8

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)
Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
  • Ex. `d.ToString("F6");` will give `5.000000` - which is what helped me to write out a decimal from a `numeric` field in the database to string. `d.ToString("0.00");` gave me `cannot convert from double to string` https://stackoverflow.com/questions/533931/convert-double-to-string – vapcguy Sep 18 '18 at 22:37
2

You can use decimal.Tostring() method

pls go through this link for more info

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
1

Use, decimal.ToString() method. You can specify format with that method if you need:

decimal d = 120.00;
string ds = d.ToString("#,#.00#");
// ds is a formated string of d's value
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292