Reputation: 93
I am trying to put comma's on a number without striping any numbers after the decimal point.
number
12018093.1000
results
12,018,093.10
I tried this code but it strips away the last 0 which I don't know why.
rps_amount_f = ("{:,}".format(float(rps_amount_f)))
Upvotes: 0
Views: 101
Reputation: 161
You said that you only want one zero so really isn't the solution just to add a 0 to the end of the string? Anyway here's my solution:
if("." in str(rps_amount_f)):
rps_amount_f = ("{:,}".format(float(rps_amount_f)) + "0")
else:
rps_amount_f = ("{:,}".format(float(rps_amount_f)))
If you want two decimal places you just get rid of the if statement and round it.
Upvotes: 1
Reputation: 14502
print("{:,.4f}".format(float(rps_amount_f))) # 12,018,093.1000
https://docs.python.org/3/library/string.html#format-specification-mini-language
Upvotes: 0
Reputation: 658
I'm not sure why the zeros are stripped away in your example, although this may be a solution using an f-string:
rps_amount_f = 12018093.1
rps_amount_f_str = f"{rps_amount_f:,.10f}"
Here '10' before the f is the decimal precision you want to have in the string, i.e. 10 decimals in this case.
Upvotes: 1