Ask Your Question
2

f string float not working

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

cybervigilante gravatar image

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

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__
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

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

slelievre gravatar image

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

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

Comments

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 -0500 )edit

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 ( 2020-07-22 13:36:24 -0500 )edit
0

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

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

Stats

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

Seen: 112 times

Last updated: Jul 22