Skip to content

Instantly share code, notes, and snippets.

@rpivo
Created February 25, 2021 16:52
Show Gist options
  • Save rpivo/f2df3f7d851e8ddac07aac1962795ac2 to your computer and use it in GitHub Desktop.
Save rpivo/f2df3f7d851e8ddac07aac1962795ac2 to your computer and use it in GitHub Desktop.
Truncating a Float in Python

Truncating a Float in Python

from decimal import Decimal as D, ROUND_DOWN

f = 1.2345

print(
  D(f).quantize(D('0.01'), rounding=ROUND_DOWN) # 1.23
)

D('0.01') truncates the float to two decimal places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment