Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round a floating-point number down to the nearest integer?

I want to take a floating-point number and round it down to the nearest integer. However, if it's not a whole, I always want to round down the variable, regardless of how close it is to the next integer up. Is there a way to do this?

like image 952
Anthony Perez Avatar asked Jun 17 '13 07:06

Anthony Perez


People also ask

How do you round a float to an int?

round() method, which converts float to its nearest integer by adding +0.5 to it's value and then truncating it.

Which method returns the floating point value down to the nearest integer value?

round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function.

Does float round up or down?

Float number always round up.


2 Answers

Simple

print int(x) 

will work as well.

like image 91
Ghostwriter Avatar answered Oct 15 '22 00:10

Ghostwriter


One of these should work:

import math math.trunc(1.5) > 1 math.trunc(-1.5) > -1 math.floor(1.5) > 1 math.floor(-1.5) > -2 
like image 42
U2EF1 Avatar answered Oct 14 '22 23:10

U2EF1