Skip to content

Add float precision variable to csv.writer / reader #110852

Open

Description

@chaeynz

Feature or enhancement

Proposal:

var_float_precision = 2

def format_item(item):
        if isinstance(item, float):
            return f"{item:.(var_float_precision)f}"

I just now have the situation, that I want to print a 0 as 0.00, because the ERP system only detects it that way.
Please add the ability to define a custom precision for float numbers. Just recently the QUOTE_STRINGS was introduced into python, but when I manually format it to a 0.00 using {:.2f}".format(var)) it will obviously become a string, therefore it will print the variable in Quotes, which the ERP system again, does not detect.

Thank you!

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 14, 2023
  2. serhiy-storchaka commented on Aug 3, 2026

    @serhiy-storchaka
    Member

    gh-155097 (PR #155099) adds a formatter parameter to csv.writer which replaces the str() call. Quoting is still decided by the original value, so a formatted number stays unquoted — which is exactly what '{:.2f}'.format(v) in your own code cannot do:

    >>> csv.writer(sys.stdout, quoting=csv.QUOTE_NONNUMERIC,
    ...            formatter=lambda i, v: format(v, '.2f')).writerow([0.0, 'text', 12.3456])
    0.00,"text",12.35

    The first argument is the 0-based column index, so the precision can differ per column. Strings and None never reach the formatter.

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

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions