简体   繁体   中英

formatting the dollar sign and the decimal alignment

Hi I'm new to python and have been playing around on python for a while but I still can't get the dollar sign and the decimal to align.

cost=pack*price

discount= cost * dis #the percent discount
amountdue = discount + cost #how much they owe


print(format("Total $", "13"), format(cost,"6.2f"))
print(format("Dis $", "13"), format(discount,"6.2f"))
print(format("Amount Due: $","13"),format(amountdue, "6.2f"))

I want it to look like this:

Total        $ XXXX.XX
Dis          $   XX.XX
Amount Due:  $ XXXX.XX

Thank you!!

A better solution is to use the new formatting style:

print("{:<13}$ {:>7.2f}".format("Total", 7.34))
print("{:<13}$ {:>7.2f}".format("Dis", 7.34))
print("{:<13}$ {:>7.2f}".format("Amount Due:", 7.34))
# Total        $    7.34
# Dis          $    7.34
# Amount Due:  $    7.34

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM