如何将随机选择的数字转换为货币?

时间:2014-04-03 11:50:50

标签: python currency

对于我的乐透我有随机选择的累积奖金从随机选择的10万后,它会转到数字我将如何将其作为货币?这是我的累积奖金代码。

prize=random.randrange(100000)

print "welcome to the lottery!!!!"
time.sleep(1)
name=raw_input("What is your name?")
print name
print "welcome to the game show where you can win thousands of pounds by just ghuesing a number!!"
number=random.randrange(100)
while True:
    ghuess=input("state a number between 1-100")
    if ghuess>number:
        print "too high try again!"
    elif ghuess<number:
        print "too low try again!"
    else:
        # Jackpot, exit the loop.
        break
print "well done! ghuess you have won.."
time.sleep(1)
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print prize

3 个答案:

答案 0 :(得分:2)

您可以使用字符串格式轻松完成此操作,如下所示:

>>> print "${:,.2f}".format(prize)

示例:

>>> prize = 12345678   #just an example
>>> print "${:,.2f}".format(prize)
$12,345,678.00

希望有所帮助。

答案 1 :(得分:2)

如果您所做的只是打印奖品:

In [77]: print "{:,.2f}£".format(random.randrange(100000))
26,467.00£

或者如果您想要前面的货币符号

In [78]: print "£{:,.2f}".format(random.randrange(100000))
£80,244.00

如果您已拥有prize变量:

In [80]: print "£{:,.2f}".format(prize)
£64,058.00

Here's a good explanation of the format specification.

答案 2 :(得分:0)

只需将0.01的随机值加倍。如下所示:

prize=random.randrange(100000) * 0.01