Home > DeveloperSection > Forums > Round double type variable to two decimal places in C#.
Anupam Mishra

Total Post:135

Points:949
Posted on    January-15-2016 11:22 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 277  View(s)
Rate this:
Hi all,

I m rounding values using below code:
double _total =100.0056;
 its displaying as 100.0056 instead of 100.01  i m trying to round up for 2 decimal places but its not working.. same problem occurs when i use Datatype as  Decimal.. how can i round up exactly.

thank you.



Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    January-15-2016 11:55 PM

If you use Math.Round(double _total, NoOfPrecision) in your code then you get correct output upto no's of precision.
For consider, your code is as follows:

   double _total =Math.Round(Convert.ToDouble(100.0056), 2);
        Console.WriteLine(_total);

Output:

100.01

Similarily, for other datatype ,  you need to conversion in Round() function to passed as parameter.


Modified On Jan-16-2016 12:36:42 AM

Don't want to miss updates? Please click the below button!

Follow MindStick