• 11

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

As the title suggests, 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?

      • 1
    • A possible difficulty is that IEEE floating point formats can represent numbers so large that the grandularity is larger than 1. So that, while you can round x down rounding x+1 down will not give you the result you expect.


print int(x)

will work as well.

  • 178
Reply Report

One of these should work:

import math
> 1
> -1
> 1
> -2
  • 77
Reply Report

The // operator returns the floor of the division. Since dividing by 1 doesn't change your number, this is equivalent to floor but no import is needed. Notes:

  1. This returns a float
  2. This rounds towards -?
  • 43
Reply Report
      • 1
    • Nice addition. int(-1.1) == -1 while -1.1//1 == -2.0 however decimal.Decimal('-1.1')//1 == decimal.Decimal('-1') (as documented, claim 2 isn't true for decimal), so relying on how // behaves is not fully stable, even today.

a lot of people say to use int(x), and this works ok for most cases, but there is a little problem. If OP's result is:

x = 1.9999999999999999

it will round to

x = 2

after the 16th 9 it will round. This is not a big deal if you are sure you will never come across such thing. But it's something to keep in mind.

  • 9
Reply Report
    • @lokilindo But this has nothing to do with int(), it solely has to do with an improper use of float, as 1.9999999999999999 is rounded up to 2.0 at compile time (while int() is called on execution time). If you use the right data type for the variable, everything works as expected: int(decimal.Decimal('1.9999999999999999999999999999999999999999999999999999999')) gives 1
      • 1
    • That is because 1.9999999999999999 is actually equal to 2.0 in the internal float64 representation. I. e. it's already rounded as soon as it is parsed into a float, as a 64 bit float cannot represent that many significant digits. You can verify that with evaluating 1.9999999999999999 == 2.0. And if you suspect that the equals operation does some rounding on floats, you can compare the binary representation with struct.pack("d", 1.9999999999999999) == struct.pack("d", 2.0), which is also equal.
      • 2
    • And if that's exactly your point, then I don't see what's wrong with int(). The value is already 2.0 and it will convert it happily into 2.
      • 1
    • If OP's (or whomever reads this in the future) intention is to use the nearest integer ( and not the round-up value) for whatever reason, then it would be something to keep in mind.

If you don't want to import math, you could use:


Here's a piece of documentation:

>>> help(round)
Help on built-in function round in module __builtin__:

    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.
  • 6
Reply Report
    • Thanks for your answer. Next time you'll get a better reception if you write proper code (close parenthesis), and give some documentation.
      • 1
    • round was already discussed and rejected as an answer when this question was asked a year ago. OP wants math.floor.

If you working with numpy, you can use the following solution which also works with negative numbers (it's also working on arrays)

import numpy as np
def round_down(num):
    if num < 0:
        return -np.ceil(abs(num))
        return np.int32(num)
round_down = np.vectorize(round_down)

round_down([-1.1, -1.5, -1.6, 0, 1.1, 1.5, 1.6])
> array([-2., -2., -2.,  0.,  1.,  1.,  1.])

I think it will also work if you just use the math module instead of numpy module.

  • 3
Reply Report

Don't know if you solved this, but I just stumble upon this question. If you want to get rid of decimal points, you could use int(x) and it will eliminate all decimal digits. Theres no need to use round(x).

  • 1
Reply Report

Just make round(x-0.5) this will always return the next rounded down Integer value of your Float. You can also easily round up by do round(x+0.5)

  • 1
Reply Report

It may be very simple, but couldn't you just round it up then minus 1? For example:

> 1
  • -1
Reply Report
      • 2
    • This gives the wrong answer for whole integers. For instance, 2.0 rounded up is 2, and if you subtract 1 you get the incorrect result 1.
      • 2
    • @PascalCuoq I don't understand your problem. Do you want 1.0 as the result? Because OP clearly wanted to round then number off to the nearest integer.

Trending Tags