How to cut end by converting double to int?

I think you'll be lying on me, but I need help here ...

I have a double (4.5234234) and need it in Int, but each time without numbers after dots. So this does not mean that if his 4.01 or 4.99 he should be 4 in Int.

how to do it?

+3
source share
4 answers

Have you really tried this?

double myDouble = 4.5234234;
int result = (int)myDouble;
//at this point result is 4

This will “divide” everything after the decimal point and return only the whole digit in double.

+9
source

You can use the Math.Truncate method .

Return value

Type: System.Double

The odd part d; that is, the number that remains after any fractional digits have been discarded.

+4
source

, - , ,

          double d = 5.555;
          int a = (int)d;
0

Convet:

double d = 5.555;
int i = Convert.ToInt(d);

0

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


All Articles