# using the format function:
"the price is ${0:.2f}".format(6.5) #> 'the price is $6.50'
"the price is ${0:,.2f}".format(1234567890.12345678) #> 'the price is $1,234,567,890.12'
# alternatively using a format string:
price = 6.5
f"the price is ${price:,.2f}" #> 'the price is $6.50'
price = 1234567890.12345678
f"the price is ${price:,.2f}" #> 'the price is $1,234,567,890.12'
Feel free to use (copy-paste) this function definition into your projects:
def to_usd(my_price):
"""
Converts a numeric value to usd-formatted string, for printing and display purposes.
Param: my_price (int or float) like 4000.444444
Example: to_usd(4000.444444)
Returns: $4,000.44
"""
return f"${my_price:,.2f}" #> $12,000.71
Advanced Operations
Also reference the numeric functionality of these built-in Python modules:
Also reference the built-in round() function: .
Use to control how numbers will display when printed: