Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

round decimal values up to the nearest of 0.01? [duplicate]

Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)

How to round decimal value up to nearest 0.05 value??, the linked SO post also discusses the similar topic, but its not the output i expected.

I need to convert the decimal values like this

16.489-->16.49
16.482-->16.48
16.425-->16.43
7.67 --> 7.67 (no conversion)

I can use the below C# method to convert the values

  Math.Round(16.482*20)/20;

But this method not works for me, it gives the following results

16.489-->16.5 
16.482-->16.5 
7.67 --> 7.7
16.425-->16.45 

whats the elegant way in c# to do this.

like image 287
RameshVel Avatar asked Feb 08 '10 05:02

RameshVel


2 Answers

Math..::.Round Method (Decimal, Int32, MidpointRounding)

Rounds a double-precision floating-point value to the specified number of fractional digits. A parameter specifies how to round the value if it is midway between two other numbers.

   Math.Round(1.489,2,MidpointRounding.AwayFromZero)
like image 68
Fredou Avatar answered Oct 01 '22 06:10

Fredou


Did you try

 Math.Round(16.482*200)/200;
like image 44
Anthony Avatar answered Oct 01 '22 06:10

Anthony