Ask Your Question

f string float not working

asked 2020-07-22 18:55:11 +0200

cybervigilante gravatar image

updated 2020-07-22 19:13:44 +0200

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 19:11:21 +0200

slelievre gravatar image

updated 2020-07-22 19:20:10 +0200

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 20:28:05 +0200 )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 20:36:24 +0200 )edit

answered 2020-07-22 19:31:44 +0200

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 18:55:11 +0200

Seen: 533 times

Last updated: Jul 22 '20