Skip to content

a keep_left option #57

@zkurtz

Description

@zkurtz

This request is possibly a special case of #11, if you consider the case of "0 decimals".

Typically my main purpose for using significant figures is to avoid using more digits than I need to use to display a value. With that in mind, an operation like round(1234, 2) -> 1200 has no value. "1200" has the same number of digits as "1234". I don't find 1200 necessarily any easier to read, at least if I apply a spacer like "1,234", and it contains strictly less information than 1234.

So what I'd like to have is a keep_left option, meaning "keep all digits that are to the left of the decimal point":

round(1234, 2, keep_left=True) -> 1234
round(123.4, 2, keep_left=True) -> 123
round(12.34, 2, keep_left=True) -> 12
round(1.234, 2, keep_left=True) -> 1.2

It's easy to implement this; I currently use something like

def sigfig_keep_left(num: float | int, sigfigs=2) -> str:
    """Format a number with a given number of significant figures.
    
    Any rounding applies only to decimal digits, not to the number of digits in the integer part.
    """
    num_str = str(num)
    num_nondecimal_digits = len(num_str.split(".")[0])
    ndigits = max(sigfigs, num_nondecimal_digits)
    return sround(num_str, sigfigs=ndigits, spacer=",")

I'd like to PR this into sigfigs in some form if that functionality would be accepted, please advise!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions