相关疑难解决方法(0)

如何用Python舍入到2位小数?

我在这段代码的输出中得到了很多小数(华氏温度到摄氏温度转换器).

我的代码目前看起来像这样:

def main():
    printC(formeln(typeHere()))

def typeHere():
    global Fahrenheit
    try:
        Fahrenheit = int(raw_input("Hi! Enter Fahrenheit value, and get it in Celsius!\n"))
    except ValueError:
        print "\nYour insertion was not a digit!"
        print "We've put your Fahrenheit value to 50!"
        Fahrenheit = 50
    return Fahrenheit

def formeln(c):
    Celsius = (Fahrenheit - 32.00) * 5.00/9.00
    return Celsius

def printC(answer):
    answer = str(answer)
    print "\nYour Celsius value is " + answer + " C.\n"



main()
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何使程序围绕小数点后两位的每个答案?

python rounding

179
推荐指数
14
解决办法
50万
查看次数

Python 3.x舍入行为

我刚刚重新阅读Python 3.0中的新功能,它指出:

round()函数舍入策略和返回类型已更改.确切的中途情况现在四舍五入到最接近的偶数结果而不是零.(例如,round(2.5)现在返回2而不是3.)

圆形文件:

对于支持round()的内置类型,值被舍入到10的最接近的倍数到幂减去n; 如果两个倍数相等,则向均匀选择进行舍入

所以,在v2.7.3下:

In [85]: round(2.5)
Out[85]: 3.0

In [86]: round(3.5)
Out[86]: 4.0
Run Code Online (Sandbox Code Playgroud)

正如我所料.但是,现在在v3.2.3下:

In [32]: round(2.5)
Out[32]: 2

In [33]: round(3.5)
Out[33]: 4
Run Code Online (Sandbox Code Playgroud)

这似乎是违反直觉的,与我对四舍五入的理解相反(并且必然会绊倒人).英语不是我的母语,但在我读到这篇文章之前,我以为我知道舍入的含义是什么: - /我确信在引入v3的时候一定有一些关于这个的讨论,但是我无法找到一个很好的理由我的搜索.

  1. 有没有人能够深入了解为什么会改变这种情况?
  2. 有没有其他主流编程语言(例如,C,C++,Java,Perl, ..)这样做(对我不一致)舍入?

我在这里错过了什么?

更新:@ Li-aungYip的评论"银行家的舍入"给了我正确的搜索词/搜索关键字,我发现了这个问题:为什么.NET使用银行家的舍入作为默认值?所以我会仔细阅读.

python rounding python-3.x

157
推荐指数
5
解决办法
5万
查看次数

Python中的round()似乎没有正确舍入

round()函数的文档声明您传递一个数字,并将小数点后的位置传递给round.因此它应该这样做:

n = 5.59
round(n, 1) # 5.6
Run Code Online (Sandbox Code Playgroud)

但是,实际上,良好的旧浮点怪异爬进来,你会得到:

5.5999999999999996
Run Code Online (Sandbox Code Playgroud)

出于UI的目的,我需要显示5.6.我在互联网上搜索并发现一些文档,这取决于我的Python实现.不幸的是,这发生在我的Windows开发机器和我尝试过的每台Linux服务器上.在这里也看到.

没有创建我自己的圆形库,有什么方法可以解决这个问题吗?

python floating-point rounding

114
推荐指数
9
解决办法
21万
查看次数

为什么许多(旧)程序使用floor(0.5 +输入)而不是round(输入)?

差异存在于返回值中,我相信这些输入可以打破平局,比如这段代码:

int main()
{
    std::cout.precision(100);

    double input = std::nextafter(0.05, 0.0) / 0.1;
    double x1 = floor(0.5 + input);
    double x2 = round(input);

    std::cout << x1 << std::endl;
    std::cout << x2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

哪个输出:

1
0
Run Code Online (Sandbox Code Playgroud)

但它们最终只是不同的结果,人们选择其首选的结果.我看到很多"旧的"C/C++程序floor(0.5 + input)而不是使用round(input).

有历史原因吗?最便宜的CPU?

c++ rounding floor

80
推荐指数
4
解决办法
1万
查看次数

如何在Python中正确地舍入一半浮点数?

我正面临着一个奇怪的round()功能行为:

for i in range(1, 15, 2):
    n = i / 2
    print(n, "=>", round(n))
Run Code Online (Sandbox Code Playgroud)

此代码打印:

0.5 => 0
1.5 => 2
2.5 => 2
3.5 => 4
4.5 => 4
5.5 => 6
6.5 => 6
Run Code Online (Sandbox Code Playgroud)

我希望浮动值总是向上舍入,而是舍入到最接近的偶数.

为什么会出现这种行为,以及获得正确结果的最佳方法是什么?

我尝试使用fractions但结果是一样的.

python floating-point precision rounding python-3.x

46
推荐指数
7
解决办法
3万
查看次数

Python总是向下舍入?

我正在尝试完成一项作业,并且非常接近 python 总是将我的答案向下舍入,而不是在应该的时候向上舍入。这是我的代码:

startingValue = int(input())
RATE = float(input()) /100
TARGET = int(input())
currentValue = startingValue
years = 1

print("Year 0:",'$%s'%(str(int(startingValue)).strip() ))

while years <= TARGET :
  interest = currentValue * RATE
  currentValue += interest
  print ("Year %s:"%(str(years)),'$%s'%(str(int(currentValue)).strip()))
  years += 1
Run Code Online (Sandbox Code Playgroud)

这是我的代码输出:第0年:$10000,第1年:$10500,第2年:$11025,第3年:$11576,第4年:$12155,第5年:$ 12762第6年:$13400,第7年:$14071, 第8年: $147749年级:$15513

以下是应该输出的内容: Year 0: $10000, Year 1: $10500, Year 2: $11025, Year 3: $11576, Year 4: $12155, Year 5: $12763 , Year 6 : $13401 , Year 7: $14071, Year …

python rounding-error rounding while-loop

3
推荐指数
1
解决办法
9206
查看次数

圆形python十进制到最接近的0.05

我正试图将十进制的货币数量舍入到最接近的0.05.现在,我这样做:

def round_down(amount):
    amount *= 100
    amount = (amount - amount % 5) / Decimal(100)
    return Decimal(amount)

def round_up(amount):
    amount = int(math.ceil(float(100 * amount) / 5)) * 5 / Decimal(100)
    return Decimal(amount)
Run Code Online (Sandbox Code Playgroud)

有没有办法,我可以更优雅地做到这一点,而无需使用python Decimals处理浮点数(也许使用量化)?

python rounding

2
推荐指数
1
解决办法
1412
查看次数

如何在python中执行向上舍入或舍入浮点数

我需要以这样的方式舍入浮点数,

如果7.4到来,它应该舍入到下一个较低的数字,即7.
如果7.5或7.6来,它应该舍入到下一个更高的数字,即8

我怎样才能做到这一点?我正在使用python 2.7

python python-2.7

-4
推荐指数
1
解决办法
4216
查看次数