久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分

Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
本文介紹了將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

首先,我嘗試過這篇文章(以及其他):Python 中的貨幣格式.它對我的變量沒有影響.我最好的猜測是因?yàn)槲沂褂玫氖?Python 3,而那是 Python 2 的代碼.(除非我忽略了某些東西,因?yàn)槲沂?Python 新手).

First of all, I have tried this post (among others): Currency formatting in Python. It has no affect on my variable. My best guess is that it is because I am using Python 3 and that was code for Python 2. (Unless I overlooked something, because I am new to Python).

我想將浮點(diǎn)數(shù)(例如 1234.5)轉(zhuǎn)換為字符串,例如$1,234.50".我該怎么做呢?

為了以防萬一,這是我編譯的代碼,但不影響我的變量:

And just in case, here is my code which compiled, but did not affect my variable:

money = float(1234.5)
locale.setlocale(locale.LC_ALL, '')
locale.currency(money, grouping=True)

同樣失敗:

money = float(1234.5)
print(money) #output is 1234.5
'${:,.2f}'.format(money)
print(money) #output is 1234.5

推薦答案

在 Python 3.x 和 2.7 中,您可以簡單地這樣做:

In Python 3.x and 2.7, you can simply do this:

>>> '${:,.2f}'.format(1234.5)
'$1,234.50'

:, 添加逗號作為千位分隔符,.2f 將字符串限制為小數(shù)點(diǎn)后兩位(或添加足夠的零以達(dá)到小數(shù)點(diǎn)后兩位,視情況而定)在最后.

The :, adds a comma as a thousands separator, and the .2f limits the string to two decimal places (or adds enough zeroes to get to 2 decimal places, as the case may be) at the end.

這篇關(guān)于將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個(gè)數(shù)字在舍入到 n 個(gè)有效十進(jìn)制數(shù)字時(shí)是否幾乎相等的函數(shù))
主站蜘蛛池模板: 中文字幕欧美一区二区 | 成人在线网| 亚洲一二三在线 | 亚洲精品性视频 | 久久大| 国产精品一区二区三 | 国产aⅴ爽av久久久久久久 | 国产福利在线播放麻豆 | 亚洲最新在线 | 在线播放国产视频 | 久久免费精品视频 | 欧美一级欧美三级在线观看 | 欧美电影在线观看网站 | av中文字幕在线播放 | 免费在线观看av的网站 | 久久久久久成人 | 激情 亚洲 | 欧美日韩不卡合集视频 | 久久综合一区二区三区 | 国产亚洲成av人片在线观看桃 | 精品欧美一区二区久久久伦 | 久草精品视频 | 精品国产欧美一区二区 | 影音先锋久久 | 亚洲国产成人av | 高清黄色毛片 | 欧美日韩亚洲国产 | 成人精品一区二区 | 偷拍自拍第一页 | 久草在线高清 | 国产日韩欧美一区二区在线播放 | 蜜桃特黄a∨片免费观看 | 久草视频在线播放 | 国产精品美女久久久久久免费 | 精品国产乱码久久久 | 久久国产综合 | 亚洲第一福利网 | 宅女噜噜66国产精品观看免费 | 国产午夜精品久久久久免费视高清 | 狠狠涩 | 天天躁日日躁aaaa视频 |