python - 如何在python中用尾随零舍入数字
问题描述
在excel中,有这个公式:=ROUNDDOWN(D28-D33-D34,-2)我得到了结果==> 166400。但是没有Rounddown公式的结果是==> 166468.50。我的问题是如何在 python 中得到相同的结果。当我进行回合(n,-2)时,我得到==> 166500。任何帮助将不胜感激!谢谢你。
解决方案
这是ROUNDDOWN(x,n)
Python中函数的实现
def rounddown(x,n):
return int(x// 10**n * 10**n)
print(rounddown(166468.50,1)) #166460
print(rounddown(166468.50,2)) #166400
print(rounddown(166468.50,3)) #166000
[更新]
该函数的新版本,可以rounddown
处理. (模拟 Excel 中的 ROUNDDOWN(x,n))n
def rounddown(x,n):
sign=1 if n>0 else 0 # getting the sign of n.
n=abs(n) # getting the absolute value of n.
p= 10**n # calculating p the nth power of 10.
result= (x // p) * p + sign * p # calculating result.
return int( result )
# sample result
print(rounddown(166468.50,1)) #166470
print(rounddown(166468.50,2)) #166500
print(rounddown(166468.50,3)) #167000
print(rounddown(166468.50,0)) #166468
print(rounddown(166468.50,-1)) #166460
print(rounddown(166468.50,-2)) #166400
print(rounddown(166468.50,-3)) #166000
推荐阅读
- python - 从字符串变量中读取并使用 Python Flask 上传 .txt 文件?
- php - 刷新后重复的数据库条目
- flutter - 如何放置隐藏参数并发送下一页
- c# - 如何在asp boundfield标题文本中添加下标
- c++ - 无法使用 mingw-w64 g++ 编译:找不到
- file - 如何将文件表及其详细信息/属性导出到 Excel?
- java - 我们可以在不使用 NDK 的情况下在 Android Studio 中执行 C 程序吗?
- dialogflow-es - Dialogflow - 情绪分析 - SentimentAnalysisResult obj 有时为空
- excel - 在即时窗口中显示变量数组值
- vue.js - 无法通过 vuejs 将数组打印到表格