ROUND DOUBLE TYPE VARIABLE TO TWO DECIMAL PLACES IN C#.

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
C#  .NET 
C#
 540  View(s)
Ratings:
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.

  1. Manoj Bhatt

    Post:154

    Points:1086
    Re: Round double type variable to two decimal places in C#.

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!