Skip to content

Implement remaining string format codes for floats #1656

Closed

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

  1. added
    C-compatA discrepancy between RustPython and CPython
    on Jan 2, 2020
  2. merkrafter commented on Jan 31, 2021

    @merkrafter
    Contributor

    Please update the current status of this issue.
    It looks like e.g. :g and :G are supported meanwhile (though not yielding the same results as CPython does in every case in the master branch as of writing this (which is now addressed in #2431 )).

  3. JazzGlobal commented on Apr 7, 2023

    @JazzGlobal
    Contributor

    Im gonna go ahead and wrap up the 'n' case.

  4. DimitrisJim commented on Apr 16, 2023

    @DimitrisJim
    Member

    Thanks all who contributed, closing issue now.

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