Kiran Reddy
Kiran Reddy

Reputation: 1125

Two Decimal places using c#

decimal Debitvalue = 1156.547m;

decimal DEBITAMT = Convert.ToDecimal(string.Format("{0:0.00}", Debitvalue));

I have to get only two decimal places but by using this code I am getting 1156.547. Which format do I have to use to display two decimal places?

Upvotes: 93

Views: 403548

Answers (14)

sami ullah
sami ullah

Reputation: 1094

double doubleNumber= 1.1; // 7.25 // 700.0
string uptwoDecimalPlaces= number.ToString("F2");

Upvotes: -1

Zuber Khan
Zuber Khan

Reputation: 79

To display two decimal digits, try the given syntax.

string.Format("{0:0.00}", Debitvalue)

Upvotes: 0

Kabilan Smart
Kabilan Smart

Reputation: 186

Another way :

decimal.Round(decimalvalue, 2, MidpointRounding.AwayFromZero);

Upvotes: 8

WoofWoof88
WoofWoof88

Reputation: 1145

Your question is asking to display two decimal places. Using the following String.format will help:

String.Format("{0:.##}", Debitvalue)

this will display then number with up to two decimal places(e.g. 2.10 would be shown as 2.1 ).

Use "{0:.00}", if you want always show two decimal places(e.g. 2.10 would be shown as 2.10 )

Or if you want the currency symbol displayed use the following:

String.Format("{0:C}", Debitvalue)

Upvotes: 83

George Wurthmann
George Wurthmann

Reputation: 489

If someone looking for a way to display decimal places even if it ends with ".00", use this:

String.Format("{0:n1}", value)

Reference:

https://learn.microsoft.com/pt-br/dotnet/standard/base-types/standard-numeric-format-strings#the-numeric-n-format-specifier

Upvotes: 0

Rodrigo Rodrigues
Rodrigo Rodrigues

Reputation: 34

In some tests here, it worked perfectly this way:

Decimal.Round(value, 2);

Hope this helps

Upvotes: -1

AH.
AH.

Reputation: 3091

Probably a variant of the other examples, but I use this method to also make sure a dot is shown before the decimal places and not a comma:

someValue.ToString("0.00", CultureInfo.InvariantCulture)

Upvotes: 4

Md. Shafiqur Rahman
Md. Shafiqur Rahman

Reputation: 3058

For only to display, property of String can be used as following..

double value = 123.456789;
String.Format("{0:0.00}", value);

Using System.Math.Round. This value can be assigned to others or manipulated as required..

double value = 123.456789;
System.Math.Round(value, 2);

Upvotes: 13

kevininhe
kevininhe

Reputation: 617

The best approach if you want to ALWAYS show two decimal places (even if your number only has one decimal place) is to use

yournumber.ToString("0.00");

Upvotes: 43

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

Use Math.Round() for rounding to two decimal places

decimal DEBITAMT = Math.Round(1156.547m, 2);

Upvotes: 64

Esteban Perez
Esteban Perez

Reputation: 428

I use

decimal Debitvalue = 1156.547m;
decimal DEBITAMT = Convert.ToDecimal(string.Format("{0:F2}", Debitvalue));

Upvotes: 22

Dimitar Tsonev
Dimitar Tsonev

Reputation: 3882

Another option is to use the Decimal.Round Method

Upvotes: 2

COLD TOLD
COLD TOLD

Reputation: 13579

here is another approach

decimal decimalRounded = Decimal.Parse(Debitvalue.ToString("0.00"));

Upvotes: 16

cjk
cjk

Reputation: 46415

If you want to round the decimal, look at Math.Round()

Upvotes: 43

Related Questions