Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

So I know when I type

print(f'{balance:2,.2f}')

it's going to put the commas in the right spot for my numbers, but I would like to know why that is. I know that the 2f makes the decimal fixed at two places to the right, but why does the comma go over 3 to the left of the decimal and every 3 after?

asked in CSC201 Spring 2021 by (4.8k points)

2 Answers

+4 votes

The comma (",") in the f-string works as a thousand separator. So if your number is going to be million, billion, or even trillion, it will still separate each group of three numbers by a comma

answered by (8 points)
+4 votes

Because thats the convention for commas in big numbers. Theres no way to change that, its always 3.

answered by (8 points)
...