What is the equivalent of the Excel ROUNDDOWN function (number, num_digits) in C #?

As the name implies, I need the C # equivalent of ROUNDDOWN.

For example, if you take the figure 13.608000, the output I'm looking for is 13.60.

I can't seem to find anything that covers what I want.

+3
source share
6 answers

You can do the following:

var rounded = Math.Floor(13.608000 * 100) / 100;

Note that Math.Floor () is rounded to the nearest integer, therefore, you must multiply, round, and then divide.

+7
source

Here's the direct port of the Excel function for a variable number of decimal places

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

eg. RoundDown (13.608000,2) = 13.60, RoundDown (12345, -3) = 12000,

+7
source

:

    double RoundDown(double value, int digits)
    {
        if (value >= 0)
            return Math.Floor(value * Math.Pow(10, digits)) / Math.Pow(10, digits);

        return Math.Ceiling(value * Math.Pow(10, digits)) / Math.Pow(10, digits);
    }

RichardW1001 , .

+2

Math.Floor. , 1,0, Floor .

double x = 0.01 * Math.Floor(100 * y);
0
source

The Math.Round function should do this, http://msdn.microsoft.com/en-us/library/zy06z30k.aspx

-1
source

Workaround:

decimal x = 13.6080001;
int places = 2;
int result = (int)(Math.Round(x - (0.5 * Math.Pow(10, 0 - places), places)));
-1
source

Source: https://habr.com/ru/post/1775924/


All Articles