Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 四舍五入加载杆的百分比,但将[99-100]捕捉到99%,将(0,1]捕捉到1%_Python_Number Formatting - Fatal编程技术网

Python 四舍五入加载杆的百分比,但将[99-100]捕捉到99%,将(0,1]捕捉到1%

Python 四舍五入加载杆的百分比,但将[99-100]捕捉到99%,将(0,1]捕捉到1%,python,number-formatting,Python,Number Formatting,我有一个介于0和1(含1)之间的浮动,我正在按百分比打印: complete = 999 total = 1000 print(f"Completed {complete} out of {total} ({complete / total:.0%})") 但当我真的接近(但不是完全接近)100%时,它会跳枪并打印100%,这不是用户从加载屏幕上期望的: 完成了1000人中的999人(100%) 我希望上面的数字是99%,尽管它确实达到了100%。同样,如果我完成了1/

我有一个介于0和1(含1)之间的浮动,我正在按百分比打印:

complete = 999
total = 1000

print(f"Completed {complete} out of {total} ({complete / total:.0%})")
但当我真的接近(但不是完全接近)100%时,它会跳枪并打印100%,这不是用户从加载屏幕上期望的:

完成了1000人中的999人(100%)

我希望上面的数字是99%,尽管它确实达到了100%。同样,如果我完成了1/1000,我想说我完成了1%,而不是什么都没有(0%)。

这里有一种方法:

complete = 999 
total = 1000

pct = math.floor(complete * 100.0/total)/100
if complete / total >= 0.001:
    pct = max(pct, 0.01)
    
print(f"Completed {complete} out of {total} ({pct:.0%})")
输出:

Completed 999 out of 1000 (99%)
如果
complete
为1,它将打印1%,即使它接近0

更完整的解决方案 遵循相同理性的更全面的解决方案是,将所有内容的四舍五入计算到50%,然后将所有内容的四舍五入计算到50%到100%:

def get_pct(complete, total):
    
    pct = (complete * 100.0 / total)
    if pct > 50: 
        pct = math.floor(pct) /100
    else:
        pct = math.ceil(pct) /100
    return pct

complete = 1
total = 1000
print(f"Completed {complete} out of {total} ({get_pct(complete, total):.0%})")
#==> Completed 1 out of 1000 (1%)

complete = 999
total = 1000
print(f"Completed {complete} out of {total} ({get_pct(complete, total):.0%})")
#==> Completed 999 out of 1000 (99%)

complete = 555
total = 1000
print(f"Completed {complete} out of {total} ({get_pct(complete, total):.0%})")
#==> Completed 555 out of 1000 (55%)

complete = 333
total = 1000
print(f"Completed {complete} out of {total} ({get_pct(complete, total):.0%})")
#==> Completed 333 out of 1000 (34%)
我是这样做的:

def format_loading_percent(f, ndigits=0):
    """Formats a float as a percentage with ndigits decimal points 
    but 0.001 is rounded up to 1% and .999 is rounded down to 99%."""
    limit = 10 ** -(ndigits + 2)
    if limit > f > 0:
        f = limit
    if 1 > f > (1 - limit):
        f = 1 - limit
    return f"{f:.{ndigits}%}"
用法示例:

format_loading_percent(0.01)                 # '1%'
format_loading_percent(0.001)                # '1%'
format_loading_percent(0.000001, ndigits=2)  # '0.01%'
format_loading_percent(0.999)                # '99%'
format_loading_percent(0.995)                # '99%'
format_loading_percent(0.991)                # '99%'
编辑:仔细想想,打印
99%
更为正确:

def format_loading_percent(f, ndigits=0):
    limit = 10 ** -(ndigits + 2)
    if limit > f > 0:
        return f"<{limit:.{ndigits}%}"
    if 1 > f > (1 - limit):
        return f">{1 - limit:.{ndigits}%}"
    return f"{f:.{ndigits}%}"
def格式加载百分比(f,ndigits=0):
限值=10**-(ndigit+2)
如果限制>f>0:
返回f“{1-限制:.{ndigits}%}”
返回f“{f:.{ndigits}%}”
这个问题类似于