在Python浮点操作中将小数位数限制为两位 [英] Restrict number of decimal places to two in Python float operations
问题描述
1.00
,然后乘以一个常数。 随后传递给一个函数,决定它是否是一个有效的回文。
在不是的情况下,数字会逐渐增加,直到达到有效的回文数 - 但是,我想限制探索空间小数点后第二位
也就是说, 下面的代码执行上述过程,注意当前输出通常是无效的,溢出到更小的小数空间,即 操作的小数位数如何限制为两位? 建议此处的解决方案似乎不起作用: / p> My idea is accept a number as input, perhaps 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 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. How can the number of decimal places of the operation be restricted to two? The solution suggested here does not seem to work:
Got it like this:
这篇关于在Python浮点操作中将小数位数限制为两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 1.01
是有效的输出,但是 1.001 $ c $不是。
$ 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
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(坦白纪元))
1.00
and then multiply it by a constant. 1.01
is valid output, but 1.001
is not. 1.00001
and so on. 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) )
# check whether we have a Palindrome
while not isPalindrome(format(round(candidrome,2))):
candidrome += 0.01
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) )