Home > database >  Quickly formatting numbers in print() calls with tuples mixing strings and numbers
Quickly formatting numbers in print() calls with tuples mixing strings and numbers

Time:01-13

When writing quick code notes and troubleshooting issues in my code, I often write notes to myself which are to be printed when certain parts of my code are reached. These notes often calculate some variable, which I print in order to check that things are WAI. The quickest and most intuitive way to write these, that I've found, are tuples mixing string and variables (typically referring to integers/floats). For example:

>>> val1=4
>>> val2=6
>>> print('The first value shows',val1,'but the second value gives',val2)
The first value shows 4 but the second value gives 6

There are times when I have very large values, or values with many decimals. I'd like to make these more readable. For example:

>>> val1=4.351324*10**8
>>> val2=6.2151234623431435235
>>> print('The first value shows',val1,'but the second value gives',val2)
The first value shows 435132400.0 but the second value gives 6.2151234623431435

But what I'd rather do is format these numbers, so that the statement becomes more readable. I'd like it to return something like this:

The first value shows 435,132,400 but the second value gives 6.215

Where I've inserted commas and made the first value an integer if it's an integer, and the second was rounded to 3 decimal points. This can, of course, be done using string formatting, but I've found this to be tedious - especially for statements I'm just using to troubleshoot things (rather than for production purposes). To get the desired results, I'd have to do something like:

>>> print('The first value shows {0:,} but the second value gives {1:5.3f}'.format(int(val1),val2))

But this has several drawbacks:

  1. This is less readable. It's quite simple to read "'The first value shows',val1,..." chronologically. Using the .format method requires I include my variable tuple at the end and spend more time thinking about what values are going where. When I'm writing these lines, I'm typically trying to troubleshoot something and not interested in creating additional potential error points.
  2. As I mentioned before, this is tedious. Tedious tasks are what code is for, so I'd like to be able to set the number format someplace, then call it. This is particularly important if the number returned is unexpected (which is definitely possible as I search for errors). For example, I may expect a number to be an integer, but it is a float instead. If I format it into an integer, I will miss this issue and not find my error.

Ideally I'd have a format that I can apply to every number in printed tuples, which is some sort of if/then statement. Ex: for number n, if n is an integer, use formatting f1, if n is a float, use formatting f2, if n>X use scientific notation (f3), if n is small and nonzero, but appears zero using f2, use scientific notation. This could be called for everything that's a number in a given print function, or could be applied throughout the document.

If having an if/then sequence to handle any numeric cases is impossible, I'd also settle for just applying one of these to all values in a tuple (ex: use comma separators) without having to individually format each number.

As it stands, I'd rather be fast than well-formatted (so I don't format these kinds of notes), but I'd love to do both. Is that possible?

CodePudding user response:

from typing import Any

def standarize(value: Any) -> str:
    if type(value) == int:
        return str(value)
    elif type(value) == float:
        return f'{value:.2f}'
    else:
       return str(value)
    
print(f'Value int: {standarize(1)}')
print(f'Value float: {standarize(113.91715)}')
print(f'Value str: {standarize("any string")}')
  •  Tags:  
  • Related