If I have a decimal
, how do I get a string version of it with two decimal places? This isn't working:
Math.Round(myDecimal, 2).ToString("{0.00}");
If I have a decimal
, how do I get a string version of it with two decimal places? This isn't working:
Math.Round(myDecimal, 2).ToString("{0.00}");
Don't use the curly brackets, they are for embedding a formatted value in a longer string using string.Format
. Use this:
myDecimal.ToString("0.00");
Maybe i'm wrong, but i've tried myDecimal.ToString(); and it worked.
Assuming myDecimal
is a System.Decimal
, then Math.Round(myDecimal, 2).ToString();
will display two decimal digits of precision, just as you want, without any format string (unless the absolute value of your number is greater than 10^27-1). This is because the decimal
datatype retains full precision of the number. That is to say that 1m
, 1.0m
, and 1.00m
are all stored differently and will display differently.
Note that this is not true of float
or double
. 1f
, 1.0f
, and 1.00f
are stored and display identically, as do 1d
, 1.0d
, and 1.00d
.
Since the format string must be parsed at runtime, I would probably omit it for code like this in most cases.