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

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

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

問題描述

首先,我嘗試過這篇文章(以及其他):Python 中的貨幣格式.它對我的變量沒有影響.我最好的猜測是因為我使用的是 Python 3,而那是 Python 2 的代碼.(除非我忽略了某些東西,因為我是 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).

我想將浮點數(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ù)點后兩位(或添加足夠的零以達到小數(shù)點后兩位,視情況而定)在最后.

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)于將浮點數(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 浮點小數(shù)點/精度)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
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(用于確定兩個數(shù)字在舍入到 n 個有效十進制數(shù)字時是否幾乎相等的函數(shù))
主站蜘蛛池模板: 久久精品国产视频 | 欧美日韩无 | 国产精品久久久一区二区 | 中文字幕在线日韩 | 国产做受网站 | 两性午夜视频 | 夜夜欢视频 | 免费黄色片网站 | 欧美亚洲视频 | 亚洲精品免费看 | 欧美性猛交xxxx乱大交退制版 | 在线一区二区三区 | 日韩精品一区二区三区中文在线 | 精品久久久久久一区二区里番 | 免费成人深夜夜国外 | 亚洲免费视频一区 | 午夜99 | www.日本高清 | 亚洲一区二区三区 | 精品日韩一区二区三区 | 国产一级免费观看 | 亚洲一区久久 | 国产精品视频久久久 | 中国极品少妇xxxx做受 | 成人高潮片免费网站 | 可以免费看的毛片 | 亚洲黄色一级 | 国产精品成人一区二区三区 | 黄色一级大片在线免费看国产一 | 亚洲成人欧美 | 久久中文字幕视频 | 亚洲成人久久久 | 欧美视频区| 日韩黄网 | 国产香蕉在线观看 | 成人性生活视频 | 午夜网址 | 日韩成人精品一区二区 | 国产精品欧美一区二区 | 欧美黑人一区二区三区 | 激情婷婷网 |