如何找到一个数字中的最低位值

huangapple go评论65阅读模式
英文:

How to find the lowest place value in a number

问题

我正在编写一个考虑百分比误差的程序,其中一件我想要做的事情是找到一个数字中的最小位数。

示例:

  • 1000 的最小位数是 1
  • 12.0 的最小位数是 0.1
  • 3.1415 的最小位数是 0.0001
  • 等等...

是否有内置函数可供使用,或者我可以编写一个简单的操作?

英文:

I am writing a program that takes percent errors into consideration and one thing I would like to be able to do is find the lowest-place value in a number.

Example:

1000 has the lowest place of 1    
12.0 has the lowest place of 0.1    
3.1415 has the lowest place of 0.0001    
etc...

Is there a built-in function I could use or is there a simple operation I could write?

答案1

得分: 3

以下是代码部分的中文翻译:

无内置函数

但此函数能够找到您要查找的值。

def e_reformat(s_number: str) -> str:
    try:
        e_index = s_number.index("e")
    except ValueError:
        return s_number
    e_number = int(s_number[e_index + 1 :])
    prefix_number = s_number[:e_index].replace(".", "")
    return f'0.{"0"*(-e_number-1)}{prefix_number}';

def lowest_place(number: float | str) -> str:
    s_number = e_reformat(str(number).lower())
    try:
        dot_index = s_number.index(".")
    except ValueError:
        return "1";
    after_dot = s_number[dot_index + 1 :]
    return f"0.{'0' * (len(after_dot) - 1)}1";

测试:

>>> lowest_place(1000)
'1'
>>> lowest_place(12.0)
'0.1'
>>> lowest_place(3.1415)
'0.0001'
>>> lowest_place('1.00')
'0.01'
>>> 1.2e-9 == 0.0000000012, lowest_place(1.2e-9) == lowest_place('0.0000000012')
(True, True)

⚠️lowest_place(1.)lowest_place('1.') 返回 0.1。如果您期望值为 1,请改用以下的 lowest_place 函数:

def lowest_place(number: float | str) -> str:
    s_number = e_reformat(str(number).lower())
    try:
        dot_index = s_number.index(".")
    except ValueError:
        return "1";
    after_dot = s_number[dot_index + 1 :]

    if not after_dot:
        return "1";

    return f"0.{'0' * (len(after_dot) - 1)}1";
英文:

No builtin function

But this function is able to find the value you are looking for.

def e_reformat(s_number: str) -> str:
    try:
        e_index = s_number.index("e")
    except ValueError:
        return s_number
    e_number = int(s_number[e_index + 1 :])
    prefix_number = s_number[:e_index].replace(".", "")
    return f'0.{"0"*(-e_number-1)}{prefix_number}'

def lowest_place(number: float | str) -> str:
    s_number = e_reformat(str(number).lower())
    try:
        dot_index = s_number.index(".")
    except ValueError:
        return "1"
    after_dot = s_number[dot_index + 1 :]
    return f"0.{'0' * (len(after_dot) - 1)}1"

Tests:

>>> lowest_place(1000)
'1'
>>> lowest_place(12.0)
'0.1'
>>> lowest_place(3.1415)
'0.0001'
>>> lowest_place('1.00')
'0.01'
>>> 1.2e-9 == 0.0000000012, lowest_place(1.2e-9) == lowest_place('0.0000000012')
(True, True)

⚠️lowest_place(1.) or lowest_place('1.') returns '0.1'. If you expect the value to be '1', use this lowest_place function instead:

def lowest_place(number: float | str) -> str:
    s_number = e_reformat(str(number).lower())
    try:
        dot_index = s_number.index(".")
    except ValueError:
        return "1"
    after_dot = s_number[dot_index + 1 :]

    if not after_dot:
        return "1"

    return f"0.{'0' * (len(after_dot) - 1)}1"

huangapple
  • 本文由 发表于 2023年3月9日 19:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定