Equivalent to Excel INT

I am looking for the C # equivalent of the following Excel formula:

(INT(((4800/1000)/0.415)*2.393)/0.05)

I get the value 540with the above formula in Excel, but now I'm trying to do the same in C # with the following code:

Math.Round((((4800 /1000) / 0.415) * 2.393) / 0.05, 0)

I get the value 553.

How can I get the same value with my C # coding?

+4
source share
3 answers

What you need:

(int)(((4800.0 /1000) / 0.415) * 2.393) / 0.05

Or alternatively:

(int)((((double)4800 / 1000) / 0.415) * 2.393) / 0.05

The problem is that 4800 and 1000 literals are interpreted as integers, and in C # dividing two integers, you get another rounded integer. So 4800/1000 = 4, but you want 4.8.

.0, double, . .

+5

:

using System;

public class Test
{
    public static void Main()
    {
        Console.WriteLine((int)((((double)4800 / 1000) / 0.415) * 2.393) / 0.05);
    }
}

IDEONE DEMO

, Integer (4800/1000), (4) excel (4.8), . .

+2

, Excel

((4800 /1000) / 0.415) * 2.393

- 27, , 0,05, 540.

#, ... Konamiman , int rounding. .0 , , ints:

Math.Round((((4800.0 / 1000.0) / 0.415) * 2.393) / 0.05, 0);

calc, , # calc ( , ) :

enter image description here

EDIT:

, .

Math.Floor, .

double d = ((4800.0 / 1000.0) / 0.415) * 2.393;
// d = 27.678...
double d1 = Math.Floor(d);
// d1 = 27
double d2 = d / 0.05;
// d2 = 540

Note . Be careful with the last line ... if you use a divisor other than where the converse is a pure integer, you will not get a round number for d2. Therefore, you may need to use Math.Round or Math.Floor on d2 to fix this ... it all depends on whether the 0.05 changes.

+1
source

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


All Articles