How can I round the number to the nearest value of lower modulus in C #. For example, in Excel there is a formula for this: = ROUND (number; number of digits).

For example, round the number 1 545 978, 80 -> 1 545 000.00

  • one
    @AK, look exactly how he wants to round off - Grundy
  • @Grundy By the way, yes. Topikstarter, and you need to round up to the nearest whole right? Well, and not to get up two times: negative numbers can be, if so, will their rounding rule be in the direction of minus infinity? - AK
  • To the nearest smaller module. The numbers are only positive. Just get this. I have a written program, there is a sum, which takes a double value when calculating, but you can also output an integer. So, for example, the amount can be as in the example of 1 545 978 rubles. And on the document I need to specify already 1 545 000. Now it is done in Excel, but from there we go to the software. - Andrey Sherman
  • one
    You should not count money in double , never-never. For financial calculations only decimal . - VladD

4 answers 4

There is no built-in function to perform this action. But you can always write your own, for example:

 double roundDown(double number, int p) { return Math.Round(number - number % Math.Pow(10, p)); } 

Call example

  roundDown(1545978.80, 3) // 1545000.0 

How it works?

Let the initial number 1545978.80 and need to get 1545000.00

To get from the original number, subtract 978.80 .

This is the remainder of dividing the original number by 1000, which in turn is 10 3 , where 3 is the number of zeros before the comma.

  • What you need and it works. You can tell, if it is not difficult how return Math.Round (number - number% Math.Pow (10, p)) works; I've been trying to invent a whole day, still learning how to program .... my head is already not cooking. - Andrey Sherman
  • @AndreySherman, supplemented the answer, but I advise you to heed the advice @VladD - Grundy

Decimals make sense only for decimal numbers, for double concept of decimal places is meaningless .

For ordinary rounding ( Round ) there is a function out of the box: Decimal.Round(Decimal, Int32) . Decimal.Round(Decimal, Int32) .

But for rounding down you have to write yourself:

 static decimal Truncate(decimal d, int places) => Shift(Math.Truncate(Shift(d, places)), -places); static decimal Shift(decimal d, int places) { if (places >= 0) { for (int i = 0; i < places; i++) d /= 10m; } else { for (int i = 0; i < -places; i++) d *= 10m; } return d; } 
     public double Floor(double num, int rank) { if (rank == 0) return Math.Truncate(num); num /= Math.Pow(10, -rank); num = Math.Truncate(num); num *= Math.Pow(10, -rank); return num; } 
    • this is not what the author asked - Grundy
    • Why then not just Math.Truncate ? - VladD
    • Math.Truncate cuts everything down to the comma ie 10.5 will be 10. - Andrey Sherman
    • @AndreySherman: Comment related to the old version of the answer . - VladD
    • As I understand it, you are repelled from the ROUNDDOWN function in Excel . I ran the code through tests, everything seems to work as it should, including with a negative ranke - proud.pendos

    Try playing with Math.Floor (...), Math.Ceiling (...), Math.Round (...)

     public string down(double number, int digit) { var a = number; a /= Math.Pow(10, digit); a = Math.Round(a, 0); a *= Math.Pow(10, digit);; return $"{a},00"; } 

    Something like that. Crooked, but your task performs. I think it's worth digging this way.

    • Round (double Number, int Number of Digits, MidpointRounding), Round (decimal Number, int Number of Digits, MidpointRounding) - rounds the specified number to the specified number of decimal places after the comma. The third argument sets the rounding rules if the value is exactly midway between two numbers and can take values: MidpointRounding.AwayFromZero - to the nearest number in the direction of a larger value; I haven’t found anything smaller yet (I need to play ... The only option that came to my head was string.Format (); - Andrey Sherman
    • I would go through the algorithm: first I would divide the number by 10 ^ the number of tens. This would leave a comma to the left. Next, round everything up to zero. And I would multiply by 10 ^ dozens of numbers. Adding this the right amount of zeros. From the phone is not convenient at home I will write already in the language - Vyacheslav