First time here? Check out the FAQ!

Ask Your Question
2

f string float not working

asked 4 years ago

cybervigilante gravatar image

updated 4 years ago

slelievre gravatar image

Why am I getting this error with a perfectly valid f-string? SageMath is using Python 3.7.3. I've tried variants a dozen times with the same error.

val = 12.3
print(f'{val:.2f}')

TypeError                                 Traceback (most recent call last)
<ipython-input-13-b868ffed9f12> in <module>()
      1 val = RealNumber('12.3')
      2 
----> 3 print(f'{val:.2f}')

TypeError: unsupported format string passed
to sage.rings.real_mpfr.RealLiteral.__format__
Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 4 years ago

slelievre gravatar image

updated 4 years ago

This is a known bug, tracked at

Two workarounds exist:

  • either convert to a float:

    sage: val = 12.3
    sage: print(f'{float(val):.2f}')
  • or use a raw float from the start

    sage: val = 12.3r
    sage: print(f'{val:.2f}')
Preview: (hide)
link

Comments

Thanks. I'm using Sagemath 9.0 . Would upgrading to 9.1 help or is it still not fixed?

cybervigilante gravatar imagecybervigilante ( 4 years ago )

Looks like it still wasn't fixed. Back to forcing a float. Is sagemath choking on f strings in any other way? https://trac.sagemath.org/query?miles...

cybervigilante gravatar imagecybervigilante ( 4 years ago )
0

answered 4 years ago

Emmanuel Charpentier gravatar image

This is know and a relevant ticket is open.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 4 years ago

Seen: 705 times

Last updated: Jul 22 '20