-4
Decimal money = 7;

var result=money.Tostring("C);

result displays $7.00

How can i show result as "7.00"( with 2 numerics after point) without currency symbol?

6 Answers6

3

"C" Format Specifier is for currency representation of numbers. It uses your CurrentCulture's CurrencySymbol and CurrencyDecimalSeparator properties as a result. (If you don't use IFormatProvider as a second parameter, of course..)

You can use N2 format insead. Like;

decimal money = 7;
money.ToString("N2").Dump(); //7.00

or

Console.WriteLine(money.ToString("N2"));

Read:

Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • What is `Dump()`? I did not find an MSDN reference on it. http://msdn.microsoft.com/en-CA/library/system.string.aspx – Victor Zakharov Jun 09 '14 at 14:16
  • @Neolisk Sorry, I should mentioned it. `.Dump()` is an extension method of [LINQPad](http://www.linqpad.net/). http://stackoverflow.com/questions/3555317/linqpad-extension-methods – Soner Gönül Jun 09 '14 at 14:18
1
var result = money.ToString("0.00");
Jason Allen
  • 803
  • 5
  • 12
1

Standard format can do this:

var result = money.Tostring("N2");

Reference:

Look under "N" or "n".

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
0

Try this :

var result = money.ToString("f");
Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
0

Use N2 in ToString like

var result=money.Tostring("N2");
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

MSDN has documented the possible ways to format a decimal here. "G", "F", "N", "G2", "F2", "N2", "0.00", and others would all accomplish your task. I would check them out and see which one is right for you.

Aaron
  • 354
  • 1
  • 3
  • 13