Languages
[Edit]
EN

Python - math.floor() method example

0 points
Created by:
Lennox
785

The math.floor() function returns a value that is smaller than or equal to the argument - the result of round down operation.  

import math

print(math.floor(5.0))   # 5

print(math.floor(2.49))  # 2
print(math.floor(2.50))  # 2
print(math.floor(2.51))  # 2

print(math.floor(-2.49))  # -3
print(math.floor(-2.50))  # -3
print(math.floor(-2.51))  # -3

print(math.floor(0.999))   # 0
print(math.floor(1.001))   # 1
print(math.floor(-1.001))  # -2

1. Documentation

Syntax
math.floor(number)
Parametersnumber - number that specifies the number to round down.
Result

Rounded down number value.

Descriptionfloor is a method that takes only one parameter and returns a rounded down value.

2. Rounding with precision down-to n places example

import math


def floor_precised(number, precision):
    power = math.pow(10, precision)
    return math.floor(number * power) / power


print(floor_precised(5, 0))    # 5.0
print(floor_precised(5.0, 0))  # 5.0
print(floor_precised(.5, 0))   # 0.0

print(floor_precised(1.1234, 0))  # 1.0
print(floor_precised(1.1234, 1))  # 1.1
print(floor_precised(1.1235, 2))  # 1.12
print(floor_precised(1.1235, 3))  # 1.123

print(floor_precised(-1.1234, 0))  # -2.0
print(floor_precised(-1.1234, 1))  # -1.2
print(floor_precised(-1.1234, 2))  # -1.13
print(floor_precised(-1.1234, 3))  # -1.124

print(floor_precised(1234, -1))  # 1230.0
print(floor_precised(1234, -2))  # 1200.0
print(floor_precised(1234, -3))  # 1000.0

print(floor_precised(5_000.000_001, 0))   # 5000.0
print(floor_precised(5_000.000_001, 6))   # 5000.000001
print(floor_precised(5_000.000_001, -3))  # 5000.0

References

  1. Floor and ceiling functions - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - math module

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join