在Python浮点操作中将小数位数限制为两位 [英] Restrict number of decimal places to two in Python float operations

查看:709
本文介绍了在Python浮点操作中将小数位数限制为两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是接受一个数字作为输入,也许 1.00 ,然后乘以一个常数。

随后传递给一个函数,决定它是否是一个有效的回文。

在不是的情况下,数字会逐渐增加,直到达到有效的回文数 - 但是,我想限制探索空间小数点后第二位

也就是说, 1.01 是有效的输出,但是 1.001 $ b $ p

下面的代码执行上述过程,注意当前输出通常是无效的,溢出到更小的小数空间,即 1.00001 等等。

操作的小数位数如何限制为两位?

 汇入sys 

#这个方法决定数字是否是回文数
def isPalindrome(x):
x = str(x).replace('。','')
a,z = 0,len(x) - 1
while a< z:
如果x [a]!= x [z]:
返回False
a + = 1
z - = 1
return True

if'__main__'== __name__:

trial = float(sys.argv [1])$ ​​b
$ b candidrome = trial +(trial * 0.15)

print(candidrome)

#请检查我们是否有回文
,而不是isPalindrome(准金额):
准备金+ = 0.01

如果isPalindrome(candidrome):
print(这是一个回教徒!+ str(candidrome))

建议此处的解决方案似乎不起作用:

 #检查我们是否有一个回文数
而不是isPalindrome(格式(round(candidrome,2))):
candidrome + = 0.01


解决方案

/ p>

  import sys 

#Th方法确定数字是否是回文数
def isPalindrome(x):
x = str(x).replace('。','')
a,z = 0,len (x) - 1
,而< z:
如果x [a]!= x [z]:
返回False
a + = 1
z - = 1
return True

if'__main__'== __name__:

trial = float(sys.argv [1])$ ​​b
$ b candidrome = trial +(trial * 0.15)

print(candidrome)
candidrome = round(candidrome,2)


检查我们是否有回文
而不是isPalindrome
参加者+ = 0.01
参加者= round(参加者,2)
print(参加者)

如果isPalindrome(参加者):
print一个回教徒!+ str(坦白纪元))


My idea is accept a number as input, perhaps 1.00 and then multiply it by a constant.

Subsequently it is passed to a function that determines whether or not it is a valid palindrome.

In the case that it is not, the number will be incrementally augmented until a valid palindrome is achieved-- however, I'd like to restrict the exploration space to the 2nd decimal place.

That is to say 1.01 is valid output, but 1.001 is not.

The code that follows executes the process described above, with the caveat that the current output is typically invalid, overflowing into smaller decimal spaces, i.e. 1.00001 and so on.

How can the number of decimal places of the operation be restricted to two?

import sys

# This method determines whether or not the number is a Palindrome
def isPalindrome(x):
    x = str(x).replace('.','')
    a, z = 0, len(x) - 1
    while a < z:
        if x[a] != x[z]:
            return False
        a += 1
        z -= 1
    return True

if '__main__' == __name__:

    trial = float(sys.argv[1])

    candidrome = trial + (trial * 0.15)

    print(candidrome)

    # check whether we have a Palindrome
    while not isPalindrome(candidrome):
        candidrome += 0.01

    if isPalindrome(candidrome):
        print( "It's a Palindrome! " + str(candidrome) )

The solution suggested here does not seem to work:

# check whether we have a Palindrome
while not isPalindrome(format(round(candidrome,2))):
    candidrome += 0.01

解决方案

Got it like this:

import sys

# This method determines whether or not the number is a Palindrome
def isPalindrome(x):
    x = str(x).replace('.','')
    a, z = 0, len(x) - 1
    while a < z:
        if x[a] != x[z]:
            return False
        a += 1
        z -= 1
    return True

if '__main__' == __name__:

    trial = float(sys.argv[1])

    candidrome = trial + (trial * 0.15)

    print(candidrome)
    candidrome = round(candidrome, 2)


    # check whether we have a Palindrome
    while not isPalindrome(candidrome):
        candidrome += 0.01
        candidrome = round(candidrome, 2)
        print(candidrome)

    if isPalindrome(candidrome):
        print( "It's a Palindrome! " + str(candidrome) )

这篇关于在Python浮点操作中将小数位数限制为两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆