Q:

How to round number to constant decimal

I want to use "math.sqrt" and my output should have 4 decimal after point even for numbers like "4". is there any func or way?!
I used "round(num_sqrt, 4)" but it didn't work.
my input is like:
1
2
3
19
output must be:
1.0000
1.4142
1.7320
4.3588
and my output is:
1.0
1.4142
1.7320
4.3588

A:

Try this
from decimal import Decimal
import math

# example with sqrt function
y = Decimal(math.sqrt(4))
z = round(y, 4)
print(z) # output 2.0000

# First we take a float and convert it to a decimal
x = Decimal(16.0/7)
print(x)

# Then we round it to 4 places
output = round(x,4)
print(output) # outpun 2.2857

