How to round off decimal to two decimal places in C# ?

There are 3 ways to round off decimal to two decimal places :

1. Use ToString()
 double  d = 2.445;
 d.ToString("0.00")

Output : 2.45

2. Use Math.Round

double d = 2.445;

Math.Round(d, 2)

Output : 2.44

3. Create a Custom logic method like below :

N = 2.445 I want to show "2.45" only.

Step a. N*100 + 0.5 = 245
Step b. Divide System.Math.Floor(245) by 100
Step c. The result will be 2.45

No comments:

Popular Posts