user3492421
user3492421

Reputation:

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?

Upvotes: 0

Views: 823

Answers (2)

FuzzyDuck
FuzzyDuck

Reputation: 1521

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']

Upvotes: 2

user1907906
user1907906

Reputation:

Print each item in the list:

for i in b:
    print float_money(i)

Upvotes: 0

Related Questions