Using f-strings to format a 6-digit number with commas, round it to 1 significant figure and avoid scientific notation?

193 Views Asked by At

This is my code so far:

number = 201234
print(f'number is {number:,.0f}')

This prints: number is 201,234

However I want it to print: number is 200,000

I've tried using print(f'number is {number:,.1g}') but this prints in scientific notation like so: number is 2e+05

Is there a simple way to format this to get the desired outcome?

1

There are 1 best solutions below

2
On

Use the round() function with a negative argument.

number = 201234
print(f'number is {round(number, -5):,.0f}')

Prints

number is 200,000