我不得不为 websphere 服务器编写一些 jython 脚本。它一定是一个非常旧的 python 版本,它没有 ** 运算符或 len() 函数。我不得不使用异常来查找字符串的结尾。
无论如何,我希望这可以节省别人一些时间
def pow(x, y):
total = 1;
if (y > 0):
rng = y
else:
rng = -1 * y
print ("range", rng)
for itt in range (rng):
total *= x
if (y < 0):
total = 1.0 / float(total)
return total
#This will return an int if the percision restricts it from parsing decimal places
def parseNum(string, percision):
decIndex = string.index(".")
total = 0
print("decIndex: ", decIndex)
index = 0
string = string[0:decIndex] + string[decIndex + 1:]
try:
while string[index]:
if (ord(string[index]) >= ord("0") and ord(string[index]) <= ord("9")):
times = pow(10, decIndex - index - 1)
val = ord(string[index]) - ord("0")
print(times, " X ", val)
if (times < percision):
break
total += times * val
index += 1
except:
print "broke out"
return total
警告!- 确保字符串是数字。该功能不会失败,但您会得到奇怪且几乎可以肯定的无用输出。