1

I am trying to show just 4 decimals of a float, without rounding the number. For example, the answers that I found explain it in a way that the output is rounded up or down;

In: 1.23456789
Out: 1.2346
but my desired output is: 1.2345
from math import sqrt
n = int(input())
list1=[]
for i in range (0,n):
    list1.append(input())

for i in range (0,n):
    print("{0:.4f}".format(sqrt(int(list1[i]))))
Aksen P
  • 4,564
  • 3
  • 14
  • 27
SteveMcManaman
  • 391
  • 1
  • 17
  • 2
    As soon as you leave out digits, you are rounding. – trincot Jul 15 '19 at 19:15
  • https://stackoverflow.com/questions/8595973/truncate-to-three-decimals-in-python https://stackoverflow.com/questions/783897/truncating-floats-in-python – Josh Lee Jul 15 '19 at 19:17
  • My personal favorite, assuming `f` is the float number and `trunc` is the number of decimal digists you want to keep: `float(str(f)[:str(f).index('.')+trunc+1])` – Tomerikoo Jul 15 '19 at 20:05

1 Answers1

1

The term is truncate, for example, this is code to truncate x to four decimal places.

x = 1.23456789
print(int(x * 10000)/10000)

You'll still need to format though because you can get some inconsistencies with floating point numbers.