28

I have tried using Math.Round and MidpointRounding. This does not appear to do what I need.

Example:

52.34567 rounded to 2 decimals UP   = 52.35
 1.183   rounded to 2 decimals DOWN =  1.18

Do I need to write a custom function?

Sometimes I need a number like 23.567 to round DOWN to 23.56. In this scenario...

Math.Round(dec, 2, MidpointRounding.AwayFromZero) gives 23.57
Math.Round(dec, 2, MidpointRounding.ToEven) gives 23.57

Decimals up to 9 decimal places could come out and need to be rounded to 1, 2, 3 or even 4 decimal places.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
B-Rad
  • 1,519
  • 5
  • 17
  • 32
  • Have you tried multiplying by 100, round it, and divide by 100? – Spoike Nov 20 '12 at 21:09
  • 1
    This appeared to work for me. Maybe I'm misunderstanding the question? `Response.Write(Math.Round(52.34567, 2).ToString());` Output: 52.35 – McArthey Nov 20 '12 at 21:11
  • 1
    Obviously he hasn't tried them, as it does exactly what he needs. He can even test it out himself: `Console.WriteLine(Math.Round(52.34567, 2));` and `Console.WriteLine(Math.Round(1.183, 2));` – rossipedia Nov 20 '12 at 21:12
  • Changed my answer to handle clarified requirement – atkretsch Nov 20 '12 at 22:05

9 Answers9

48

Try using decimal.Round():

decimal.Round(x, 2)

Where x is your value and 2 is the number of decimals you wish to keep.

You can also specify whether .5 rounds up or down by passing third parameter:

decimal.Round(x, 2, MidpointRounding.AwayFromZero);

EDIT:

In light of the new requirement (i.e. that numbers are sometimes rounded down despite being greater than "halfway" to the next interval), you can try:

var pow = Math.Pow(10, numDigits);
var truncated = Math.Truncate(x*pow) / pow;

Truncate() lops off the non-integer portion of the decimal. Note that numDigits above should be how many digits you want to KEEP, not the total number of decimals, etc.

Finally, if you want to force a round up (truncation really is a forced round-down), you would just add 1 to the result of the Truncate() call before dividing again.

atkretsch
  • 2,367
  • 18
  • 24
32

Try using Math.Ceiling (up) or Math.Floor (down). e.g Math.Floor(1.8) == 1.

pascalhein
  • 5,700
  • 4
  • 31
  • 44
  • 4
    Those are good function, however, they force the value to a whole number. I need to maintain the decimals in many cases. – B-Rad Nov 20 '12 at 22:06
  • If you would like do it for decimals you can do it in that way: `Math.Floor(1.87*10)/10 == 1.8` – darson1991 Feb 19 '16 at 08:34
16

Assuming you're using the decimal type for your numbers,

static class Rounding
{
    public static decimal RoundUp(decimal number, int places)
    {
        decimal factor = RoundFactor(places);
        number *= factor;
        number = Math.Ceiling(number);
        number /= factor;
        return number;
    }

    public static decimal RoundDown(decimal number, int places)
    {
        decimal factor = RoundFactor(places);
        number *= factor;
        number = Math.Floor(number);
        number /= factor;
        return number;
    }

    internal static decimal RoundFactor(int places)
    {
        decimal factor = 1m;

        if (places < 0)
        {
            places = -places;
            for (int i = 0; i < places; i++)
                factor /= 10m;
        }

        else
        {
            for (int i = 0; i < places; i++)
                factor *= 10m;
        }

        return factor;
    }
}

Example:

Rounding.RoundDown(23.567, 2) prints 23.56
Jeff
  • 7,504
  • 3
  • 25
  • 34
  • You can also easily modify this example to make Extension methods for the decimal type e.g. public static decimal RoundDown(this decimal number, int places) – Chris W Mar 12 '19 at 13:29
10

For a shorter version of the accepted answer, here are the RoundUp and RoundDown functions that can be used:

public double RoundDown(double number, int decimalPlaces)
{
    return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
}

public double RoundUp(double number, int decimalPlaces)
{
    return Math.Ceiling(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
}
bruntime
  • 371
  • 2
  • 13
B-Rad
  • 1,519
  • 5
  • 17
  • 32
2

Complete code with result.

  double a = Math.Round(128.5, 0, MidpointRounding.AwayFromZero);

Result is 129

Kalu Singh Rao
  • 1,671
  • 1
  • 16
  • 21
Nikhil S
  • 21
  • 1
1

The Math class gives you methods to use to round up and down, they are Math.Ceiling() and Math.Floor() respectively. They work like Math.Round(), but they have a particularity, they only receive a value and round them to only the entire part.

So you need to use Math.Pow() to multiply the value by 10 to the n-esimal units you need to round power and then you need to divide by the same multiplied value.

Is important that you note, that the input parameters of the Math.Pow() method are double, so you need to convert them to double.

For example:

When you want to round up the value to 3 decimals (supposing value type is decimal):

double decimalsNumber = 3;
decimal valueToRound = 1.1835675M;
// powerOfTen must be equal to 10^3 or 1000.
double powerOfTen = Math.Pow(10, decimalsNumber);
// rounded must be equal to Math.Ceiling(1.1835675 * 1000) / 1000
decimal rounded = Math.Ceiling(valueToRound * (decimal)powerOfTen) / (decimal)powerOfTen;


Result: rounded = 1.184

When you want to round down the value to 3 decimals (supposing value type is decimal):

double decimalsNumber = 3;
decimal valueToRound = 1.1835675M;
// powerOfTen must be equal to 10^3 or 1000.
double powerOfTen = Math.Pow(10, decimalsNumber);
// rounded must be equal to Math.Floor(1.1835675 * 1000) / 1000
decimal rounded = Math.Floor(valueToRound * (decimal)powerOfTen) / (decimal)powerOfTen;


Result: rounded = 1.183

To reference how to use them more specificaly and to get more information and about both methods you can see these pages from the oficial MSDN Microsoft site:

Math Class

Math.Pow Method (Double, Double)

Math.Floor Method (Decimal)

Math.Floor Method (Double)

Math.Ceiling Method (Decimal)

Math.Ceiling Method (Double)

CryogenicNeo
  • 937
  • 12
  • 25
0

Maybe this?

Math.Round(dec + 0.5m, MidpointRounding.AwayFromZero);
Pang
  • 9,564
  • 146
  • 81
  • 122
0

Try this custom rounding:

public int Round(double value)
{
    double decimalpoints = Math.Abs(value - Math.Floor(value));
    if (decimalpoints > 0.5)
        return (int)Math.Round(value);
    else
        return (int)Math.Floor(value);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anandd360
  • 298
  • 3
  • 14
0

You can achieve that by using the method of Math.Round() or decimal.Round():

Math.Round(amt)
Math.Round(amt, Int32) and other overloading methods.

decimal.Round(amt)
decimal.Round(amt, 2) and other overloading methods.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131