Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round up a number if it's not an integer?

Tags:

double

scala

ceil

I want to calculate a simple number, and if the number is not an integer I want to round it up.

For instance, if after a calculation I get 1.2, I want to change it to 2. If the number is 3.7, I want to change it to 4 and so on.

like image 929
nick shmick Avatar asked Aug 30 '15 15:08

nick shmick


People also ask

How do you round to integer?

Rounding to the Nearest Integer If 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.

Does Python round 0.5 up or down?

5 is round up for positive values and round down for negative values. For instance, both round(0.5) and round(-0.5) return 0 , while round(1.5) gives 2 and round(-1.5) gives -2 . This Python behaviour is a bit different from how rounding usually goes.

How do you round a negative number?

If x is positive, round-down is the same as round-toward-zero, and round-up is the same as round-away-from-zero. If x is negative, round-down is the same as round-away-from-zero, and round-up is the same as round-toward-zero. In any case, if x is an integer, y is just x .

How do you round up in coding?

The round() function rounds a number to the nearest whole number. The math. ceil() method rounds a number up to the nearest whole number while the math. floor() method rounds a number down to the nearest whole number.

What does it mean to round up to the nearest integer?

Rounding up, sometimes referred to as "taking the ceiling" of a number means rounding up towards the nearest integer. For example, when rounding to the ones place, any non-integer value will be rounded up to the next highest integer, as shown below: 5.01. ⇒.

How to round up numbers?

How to Round Numbers 1 Decide which is the last digit to keep 2 Leave it the same if the next digit is less than 5 (this is called rounding down) 3 But increase it by 1 if the next digit is 5 or more (this is called rounding up) More ...

How do you round off decimals in math?

Rounding Off Decimals Determine the place value to which the number is to be rounded. Identify the place value that you will round the number to. Locate the number to the right of the rounding number. Round the rounding digit up one digit if the digit to the right is greater than or equal to 5.

How do you round numbers to the nearest hundred?

When rounding numbers, you must first understand the term "rounding digit.". When working with whole numbers and rounding to the closest 10, the ​rounding digit is the second number from the right—or the 10's place. When rounding to the nearest hundred, the third place from the right is the rounding digit—or the 100's place.


1 Answers

You can use math.ceil to round a Double up and toInt to convert the Double to an Int.

def roundUp(d: Double) = math.ceil(d).toInt

roundUp(1.2) // Int = 2
roundUp(3.7) // Int = 4
roundUp(5) // Int = 5
like image 187
Peter Neyens Avatar answered Oct 14 '22 05:10

Peter Neyens