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

如何將浮點數四舍五入到某個小數位?

How to round a floating point number up to a certain decimal place?(如何將浮點數四舍五入到某個小數位?)
本文介紹了如何將浮點數四舍五入到某個小數位?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我有 8.8333333333333339,我想將其轉換為 8.84.如何在 Python 中完成此操作?

round(8.8333333333333339, 2) 給出 8.83 而不是 8.84.我是 Python 或一般編程新手.

我不想把它打印成字符串,結果會被進一步使用.有關該問題的更多信息,請查看

從示例輸出看來,他們四舍五入每月付款,這就是許多人所說的上限函數的效果.這意味著每個月支付的總金額略多于 1?12.這使得最后的付款比平時少了一點——剩下的未付余額只有 8.76.

使用正常舍入產生 8.83 的每月付款和 8.87 稍高的最終付款同樣有效.然而,在現實世界中,人們通常不喜歡他們的付款增加,因此將每筆付款四舍五入是常見的做法——它也可以更快地將錢還給貸方.

Suppose I have 8.8333333333333339, and I want to convert it to 8.84. How can I accomplish this in Python?

round(8.8333333333333339, 2) gives 8.83 and not 8.84. I am new to Python or programming in general.

I don't want to print it as a string, and the result will be further used. For more information on the problem, please check Tim Wilson's Python Programming Tips: Loan and payment calculator.

解決方案

8.833333333339 (or 8.833333333333334, the result of 106.00/12) properly rounded to two decimal places is 8.83. Mathematically it sounds like what you want is a ceiling function. The one in Python's math module is named ceil:

import math

v = 8.8333333333333339
print(math.ceil(v*100)/100)  # -> 8.84

Respectively, the floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places — so to use them for 2 decimal places the number is first multiplied by 102 (or 100) to shift the decimal point and is then divided by it afterwards to compensate.

If you don't want to use the math module for some reason, you can use this (minimally tested) implementation I just wrote:

def ceiling(x):
    n = int(x)
    return n if n-1 < x <= n else n+1

How all this relates to the linked Loan and payment calculator problem:

From the sample output it appears that they rounded up the monthly payment, which is what many call the effect of the ceiling function. This means that each month a little more than 1?12 of the total amount is being paid. That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only 8.76.

It would have been equally valid to use normal rounding producing a monthly payment of 8.83 and a slightly higher final payment of 8.87. However, in the real world people generally don't like to have their payments go up, so rounding up each payment is the common practice — it also returns the money to the lender more quickly.

這篇關于如何將浮點數四舍五入到某個小數位?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
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 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 国外成人在线视频 | 久久精品国产久精国产 | 二区三区在线观看 | 午夜视频一区 | 欧美日韩高清在线一区 | 欧美国产视频一区二区 | 丁香久久 | 性一区| 日本午夜精品一区二区三区 | 国产毛片久久久久久久久春天 | 国产探花在线精品一区二区 | 国产日韩中文字幕 | 国产精品黄色 | 高清欧美性猛交xxxx黑人猛交 | av影音资源 | 免费中文字幕 | 天堂一区二区三区四区 | 亚洲综合一区二区三区 | 欧美成人一区二区三区 | 免费看色| 欧美自拍一区 | 麻豆亚洲 | 91动漫在线观看 | 九九热在线视频 | 国产成人99久久亚洲综合精品 | 91精品国产综合久久精品 | 欧美视频免费在线 | 国产精品美女一区二区三区 | 毛片一级网站 | 日韩欧美国产成人一区二区 | 久草资源在线 | 成年人国产在线观看 | 久久精品欧美电影 | 91精品国产91久久久久久最新 | 天堂综合 | 中文字幕蜜臀av | 国产免费av在线 | 欧美中文字幕 | 久久这里只有精品首页 | 国产高清精品一区二区三区 | 久久久久久久国产 |