Python floor float: The Complete Guide

There are two types of numbers in Python.

  1. integers
  2. floating-point

The integers are either positive numbers, negative numbers, or 0. Commonly referenced as int.

The floating-point numbers can contain decimals, whereas integers cannot. To convert a number stored in a string or integer convert into a floating-point number, use the float() function.

The math.floor() function in Python rounds a number down to the nearest integer and returns the result. Let’s see how to work with floating-values with floor() function.

Python floor float

To use a floor float in Python, use the int() function as long as your numbers are positive. The int() function can round down to the next integer. Then, the int() method on the float can be cast to that value to the lower int.

data = 3.1415161
rnd = int(data)
print(rnd)

Output

3

In this example, the int() function round down the 3.1415161 value to 3.

If you want the floating-point value output like 3.0 floating value, you can use the floor division(//) operator.

import math

data = 3.1415161
rnd = data // 1
print(rnd)

Output

3.0

You can see that we got the floating-point output.

Python floor float for negative numbers

If you use the floor division operator to the negative numbers, it gives the round-up value.

import math

data = -3.1415161
rnd = data // 1
print(rnd)

Output

-4.0

That’s it for this tutorial.

Leave a Comment