简体   繁体   中英

Formatting Floating Point values to monetary amounts in python

I want to format float values to any currency here I am converting to dollar currency.

import math

def float_money(number):
    return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

print "Formatting Floating Point Values To $ Dollar Currency"

a = input("How Many Floating Point Values Your want To Enter: ")

b = [float(input("Please Enter Floating Point Values: ")) for i in range(a)]

print float_money(b)

If I didn't give that a and b variable and give b = 333.323234 then it will print and give output as $333.32....

But here in b variable all the float numbers are stored in a list like [3334.33, 3244.342, 4234.42334] . How to get output in all dollar values?

float_values = [3334.33, 3244.342, 4234.42334]
str_values = ['${0:7.2f}'.format(val) for val in float_values]

Result:

['$3334.33', '$3244.34', '$4234.42']

Print each item in the list:

for i in b:
    print float_money(i)

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