Ask Your Question

f string float not working

asked 2020-07-22 11:55:11 -0600

cybervigilante gravatar image

updated 2020-07-22 12:13:44 -0600

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

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

TypeError: unsupported format string passed
to sage.rings.real_mpfr.RealLiteral.__format__
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2020-07-22 12:11:21 -0600

slelievre gravatar image

updated 2020-07-22 12:20:10 -0600

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}')
edit flag offensive delete link more


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

cybervigilante gravatar imagecybervigilante ( 2020-07-22 13:28:05 -0600 )edit

Looks like it still wasn't fixed. Back to forcing a float. Is sagemath choking on f strings in any other way?

cybervigilante gravatar imagecybervigilante ( 2020-07-22 13:36:24 -0600 )edit

answered 2020-07-22 12:31:44 -0600

Emmanuel Charpentier gravatar image

This is know and a relevant ticket is open.

edit flag offensive delete link more

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


Asked: 2020-07-22 11:55:11 -0600

Seen: 129 times

Last updated: Jul 22 '20