### Convert to two decimal places C# Round to 2 decimal places

##
**Convert to two decimal places C# Round to 2 decimal places**

**There are various methods or you can say ways available to round a number to two decimal places. When we store values in our database, Our main concern is to have a good precision so for that the numbers are stored with lots of decimal places. There can be 6 decimal digits, 8 decimal digits, 10 decimal digits and more.**

**The main concern arises when we need to show such values on front end platforms to the end customers. They might not be interested in so much digits after decimal. So today we will learn various ways in which we can convert / round such number to two decimal places.**

Things that also matter here is how are you receiving it at your end.

If you are receiving it as string you can do it as follows.

string decimalNumber = "15.45000000";

string newDecimal= Math.Round(Convert.ToDecimal(decimalNumber, 2).ToString(CultureInfo.InvariantCulture);

Things that also matter here is how are you receiving it at your end.

If you are receiving it as string you can do it as follows.

string decimalNumber = "15.45000000";

string newDecimal= Math.Round(Convert.ToDecimal(decimalNumber, 2).ToString(CultureInfo.InvariantCulture);

**// Output of newDecimal 15.45**

**So in the process above we are actually converting string to decimal and then we are rounding it off to two decimal places.**

**Now Suppose rounding of this string is not a big concern. Your main concern is just to represent it in the form of string with two decimal places then there is another technique you can use**

**so**

**string decimalNumber = "15.45000000";**

**string newDecimal=decimalNumber.ToString("#.##");**

**//Output here also will be similar 15.45**

**The only concern is if number is 0 then output will be "" means an Empty string.**

**You can resolve this by using this trick**

**string newDecimal=**

**decimalNumber.ToString("0.##");**

**So using this trick you can very well change the presentation of the numeric string but here rouding off is not happening as it was happening in the first method.**

**For this you can also use**

**string newDecimal=decimalNumber.ToString("F");**

**Now I will tell you techniques where you already have a decimal number. The only thing you need is to round off to two decimal places.**

**for example decimal x = 45.66300005;**

**decimal newDecimal = Math.Round(x,2, MidpointRounding.AwayFromZero);**

**//Output 45.66**

**So there are lot of ways and technique that you can use to reach your requirement. I am sure one of this definitely benefit you. Tell me in comments which one did you use. You can also left your appreciations in comment section :)**

**Other important links to explore**

**round off to 2 decimal places c#, c# 2 decimal places without rounding, c# round up to 2 decimal places, string format 2 decimal places c#, convert.todecimal c# 2 decimal places**

## Comments

## Post a Comment