How do I format a variable to a standard money format? This is what I am currently doing:
total = 214.5467
print('Your total is: $' + str(total)[0:4])
Problem is, it only puts out 3 digits, meaning most of the number will be ignored. What commands do I need to use to fix this?
use:
print('Your total is: $ {:4.6f}'.format(total))
the .6 tells format to use 6 decimal places to right the 4, 4 on left 'f' = float
Use f-string.
>>> total = 214.5467
>>> print(f'Your total is: $ {total:.2f}')
Your total is: $ 214.55
>>> for word in 'f-strings are awesome'.split():
... print(f'{word.capitalize():~^20}')
...
~~~~~F-strings~~~~~~
~~~~~~~~Are~~~~~~~~~
~~~~~~Awesome~~~~~~~
(Jan-16-2019, 10:24 PM)Larz60+ Wrote: [ -> ]use:
print('Your total is: $ {:4.6f}'.format(total))
the .6 tells format to use 6 decimal places to right the 4, 4 on left 'f' = float
(Jan-16-2019, 10:39 PM)snippsat Wrote: [ -> ]Use f-string.
>>> total = 214.5467
>>> print(f'Your total is: $ {total:.2f}')
Your total is: $ 214.55
>>> for word in 'f-strings are awesome'.split():
... print(f'{word.capitalize():~^20}')
...
~~~~~F-strings~~~~~~
~~~~~~~~Are~~~~~~~~~
~~~~~~Awesome~~~~~~~
Wow! Thanks for the advice guys. I had never heard of an f-string before. I finally got a script I've been working on finished with your advice.
Output:
~~~~~~~~You~~~~~~~~~
~~~~~~~~Guys~~~~~~~~
~~~~~~~~Are~~~~~~~~~
~~~~~~Awesome!~~~~~~