1

Here is what I have tried:

>>> pi = 3.14159265
>>> format(pi, '.3f') #print 3.142 # 3 precision after the decimal point
'3.142'
>>> format(pi, '.1f') #print 3.1
'3.1'
>>> format(pi, '.10f') #print 3.1415926500, more precision than the original
'3.1415926500'
>>> format(pi, '.5f') #print 3.14159, more precision than the original
'3.14159'
>>> format(pi, '.4f') 
'3.1416'

The concern part is this:

>>> format(pi, '.3f') #print 3.142 # 3 precision after the decimal point
'3.142'
>>> format(pi, '.4f') 
'3.1416'

Where I was expecting to have 3.1415, I am getting 3.1416. Please suggest me.
The SO is showing these 2 links:
http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate
http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary
But these are not the one I am looking for.

7
  • 3
    Why is 3.1416 concerning, but 3.142 fine? Commented May 29, 2019 at 9:14
  • @user2357112 may be I neglected that part. But most of the concern was with 4th decimal value. Lol ... May be I forgot to mention it. Commented May 29, 2019 at 9:16
  • Suggest how to do what to you? Commented May 29, 2019 at 9:20
  • Why wouldn’t you want it to round in the correct direction? decimal module has a setcontext(ROUND_DOWN) which may be of use. Obviously you’d have to convert your number to a Decimal object. Commented May 29, 2019 at 9:21
  • @NChauhan May be helpful but in my case I wanted to have the correct decimal. That's why I am trying to ask this query. Otherwise everything was fine,right? Commented May 29, 2019 at 9:23

5 Answers 5

4

It's because it is rounding it, so it is just like the round function.

To fix it:

>>> l = str(pi).split('.')
>>> l[0] + '.' + l[1][:4]
'3.1415'
>>> float(l[0] + '.' + l[1][:4])
3.1415

A function version of it:

def first_n(a, b):
    l = str(a).split('.')
    return int(l[0] + '.' + l[1][:b])

And now:

print(first_n(pi, 4))

Gives:

3.1415
Sign up to request clarification or add additional context in comments.

Comments

1

You could simply strip the last character:

pi = 3.14159

print(format(pi, '.5f')[:-1]) # 3.1415

1 Comment

But then 3.141599 still prints as 3.1416.
1

I'm not sure what kind of suggestion you want, but here's a way to truncate a number to a given number of decimal places:

pi = 3.14159265

def truncate(v, places):
    return int(v * 10**places) / 10**places

print(truncate(pi, 3))  # -> 3.141
print(truncate(pi, 4))  # -> 3.1415

Comments

1

You may try this. May be exactly list the answer @U9-Forward, a little compact.

>>> str(pi)[:6]
'3.1415'

Hope this will help.

Comments

0

there is 3.1416 for format(pi, '.4f') for the same reason there is 3.142 for format(pi, '.3f') - the next digit after your desired number of digits in formatted output is at least five - it is rounded up.

2 Comments

This is not I am expecting. I know this already that my values are getting rounded up.
So what do you expect exactly? If you just want to strip the number in sense of erasing all digits after desired place, mark @U9-Forward 's answer as correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.