Skip to content

Implement remaining string format codes for floats #1656

Description

@doyshinda

Feature

Implement the g, G, e, E, n, and % format codes for floats.

  • g
  • G
  • e
  • E
  • n
  • %

A few examples from CPython:

>>> '{:g}'.format(10.0)
'10'
>>> '{:.1g}'.format(10.0)
'1e+01'
>>> '{:e}'.format(10.0)
'1.000000e+01'
>>> '{:.1e}'.format(10.0)
'1.0e+01'
>>> '{:n}'.format(999999.1234)
'999999'
>>> '{:n}'.format(1000000.1234)
'1e+06'
>>> '{:.8n}'.format(1000000.1234)
'1000000.1'
>>> '{:.11n}'.format(1000000.1234)
'1000000.1234'
>>> '{:%}'.format(10.0)
'1000.000000%'

RustPython

>>>>> '{:g}'.format(10.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Format code 'g' for object of type 'float' not implemented yet
>>>>> '{:G}'.format(10.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Format code 'G' for object of type 'float' not implemented yet
>>>>> '{:e}'.format(10.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Format code 'e' for object of type 'float' not implemented yet
>>>>> '{:E}'.format(10.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Format code 'E' for object of type 'float' not implemented yet
>>>>> '{:n}'.format(10.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Format code 'n' for object of type 'float' not implemented yet
>>>>> '{:%}'.format(10.0)
'10.0'

Python Documentation

See the section after The available presentation types for floating point and decimal values are in the docs found here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions