0

我正在尝试在 JES 中做一个学生 jython 程序的作业。我需要将我们的学生编号转换为字符串输入变量,以通过我们的函数,即 def assignment(stringID) 并将其转换为整数。确切的说明是:

第 1 步定义一个名为 id 的数组,它将您的 7 位数字存储为整数(您在数组中设置的数字无关紧要,下一步将用您的学号覆盖)。第 2 步您的学号已作为字符串传递给您的函数。您必须分开数字并将它们分配给您的数组 ID。这可以逐行手动或使用循环来完成。在将字符串存储到 id 之前,您需要将每个字符从 stringID 类型转换为整数。

我使用 int 和 float 函数尝试了很多不同的方法,但我真的被卡住了。提前致谢!

4

2 回答 2

0
>>> a = "545.2222"

>>> float(a)

545.22220000000004

>>> int(float(a))

545
于 2014-05-03T04:19:21.693 回答
0

我不得不为 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

警告!- 确保字符串是数字。该功能不会失败,但您会得到奇怪且几乎可以肯定的无用输出。

于 2018-10-21T21:42:32.283 回答