How to only keep n decimals after a float number in python without rounding ?

Published: March 07, 2023

Tags: Python;

DMCA.com Protection Status

To keep n decimals after a float number in python without rounding there are multiple solutions:

Create a float number

Let's create a float number:

import math

pi = math.pi

gives

3.141592653589793

Keep only n decimals without rounding

To keep for example 4 decimals:

n = 4

int( pi * 10**n ) / 10**n

output

3.1415

Note that

round(pi,4)

returns

3.1416