rich icon indicating copy to clipboard operation
rich copied to clipboard

[REQUEST] Control float precision for nested objects in pretty print?

Open NekoApocalypse opened this issue 2 years ago • 0 comments

I have a program that collects some statistics from a dataset. It has two outputs:

  • A human readable preview printed to the terminal. I wish all float to be printed with precision 4. i.e. 3.141592653 -> 3.1416
  • A json save file for downstream applications. The numbers should be in their original value.

Currently I am generating a "preview" version of the result with rounded floats just for pretty print, along side a "high-precision" version for json storage.

I wonder is it possible that we control the precision of floats with a parameter?

Example:

data = {
  "player": "John",
  "games_played": 255,
  "win_rate": 166 / 255, # should be 0.65098039215
}
orignal = json.dumps(data) # output with full precision
rich.pprint(data) # pretty print with full precision
rich.pprint(data, presicion=2) # pretty print with precision = 2

Should print:

{ "player": "John", "games_played": 255, "win_rate": 0.65098039215 }
{ "player": "John", "games_played": 255, "win_rate": 0.6510 }

NekoApocalypse avatar Jun 29 '22 03:06 NekoApocalypse