Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal/double to integer - round up (not just to nearest)

Tags:

How would you round up a decimal or float to an integer. For instance...

0.0 => 0 0.1 => 1 1.1 => 2 1.7 => 2 2.1 => 3 

Etc.

like image 394
cllpse Avatar asked Dec 29 '11 09:12

cllpse


People also ask

Does double to int round up or down?

Reference (thanks Rawling) Explicit Numeric Conversions Table: When you convert a double or float value to an integral type, this value is rounded towards zero to the nearest integral value.

How do you round decimals to integers?

Rounding to the Nearest IntegerIf the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.

How do you round to next whole number in C#?

Round() . If using MidpointRounding. ToEven (the default) the value is rounded to the nearest even number ( 1.5 is rounded to 2 , but 2.5 is also rounded to 2 ).


1 Answers

Simple, use Math.Ceiling:

var wholeNumber = (int)Math.Ceiling(fractionalNumber); 
like image 149
George Duckett Avatar answered Sep 22 '22 11:09

George Duckett