Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round Up a double to int

I have a number ("double") from int/int (such as 10/3).

What's the best way to Approximation by Excess and convert it to int on C#?

like image 718
markzzz Avatar asked Feb 15 '12 14:02

markzzz


People also ask

Does double to int round down?

That is, the answer is always rounding down.

Can you assign a double to an int?

Since double is bigger data type than int, you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to a 32-bit integer, anything after the decimal point is lost.

What happens when you cast a double to an int?

As we know double value can contain decimal digits (digits after decimal point), so when we convert double value with decimal digits to int value, the decimal digits are truncated.


2 Answers

Are you asking about System.Math.Ceiling?

Math.Ceiling(0.2) == 1 Math.Ceiling(0.8) == 1 Math.Ceiling(2.6) == 3 Math.Ceiling(-1.4) == -1 
like image 159
Doug McClean Avatar answered Oct 15 '22 09:10

Doug McClean


int scaled = (int)Math.Ceiling( (double) 10 / 3 ) ; 
like image 31
EursPravus Avatar answered Oct 15 '22 10:10

EursPravus