【发布时间】:2021-05-09 06:09:27
【问题描述】:
我们有一个部分工作的代码和 2 个包含不同类型自定义步骤的示例。示例 2 (Int) 有效,而示例 1 无效,因为它是向上取整而不是向下取整。
import math
def step_size_to_precision(ss):
return ss.find('1') - 1
def format_value(val, step_size_str):
precision = step_size_to_precision(step_size_str)
if precision > 0:
return "{:0.0{}f}".format(val, precision)
return math.floor(int(val))
###########################
# # example 1
step_size = "0.00000100"
quantity = 0.00725562
print(quantity)
print(format_value(quantity, step_size))
# 0.00725562
# 0.007256 <= Is rounding up instead of down. Should be 0.007255
###########################
# # example 2
# step_size = "1"
# quantity = 3.00725562
# print(quantity)
# print(format_value(quantity, step_size))
# returns 3 <= This is correct
###########################
我们如何解决它?
【问题讨论】: