Two Decimal places using c#

C#Formatting

C# Problem Overview


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. Let me know which format I have to use to display two decimal places.

C# Solutions


Solution 1 - C#

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)

Solution 2 - C#

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

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

Solution 3 - C#

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

Solution 4 - C#

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");

Solution 5 - C#

I use

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

Solution 6 - C#

here is another approach

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

Solution 7 - C#

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);

Solution 8 - C#

Another way :

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

Solution 9 - C#

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)

Solution 10 - C#

Another option is to use the Decimal.Round Method

Solution 11 - C#

To display two decimal digits, try the given syntax.

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

Solution 12 - C#

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://docs.microsoft.com/pt-br/dotnet/standard/base-types/standard-numeric-format-strings#the-numeric-n-format-specifier

Solution 13 - C#

In some tests here, it worked perfectly this way:

Decimal.Round(value, 2);

Hope this helps

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKiran ReddyView Question on Stackoverflow
Solution 1 - C#WoofWoof88View Answer on Stackoverflow
Solution 2 - C#Nikhil AgrawalView Answer on Stackoverflow
Solution 3 - C#cjkView Answer on Stackoverflow
Solution 4 - C#Kevin InfanteView Answer on Stackoverflow
Solution 5 - C#Esteban PerezView Answer on Stackoverflow
Solution 6 - C#COLD TOLDView Answer on Stackoverflow
Solution 7 - C#Md. Shafiqur RahmanView Answer on Stackoverflow
Solution 8 - C#Kabilan SmartView Answer on Stackoverflow
Solution 9 - C#AH.View Answer on Stackoverflow
Solution 10 - C#Dimitar TsonevView Answer on Stackoverflow
Solution 11 - C#samir pathanView Answer on Stackoverflow
Solution 12 - C#George WurthmannView Answer on Stackoverflow
Solution 13 - C#Rodrigo RodriguesView Answer on Stackoverflow