将 python 中数字权重的值四舍五入为上限值和下限值

Round the values with weightage of numbers in python with upper and lower values

我有一个数字,我需要得到最接近的第 1000 个值。上面和下面还有数字之间的差异,所以我可以给权重。

Example:

Number_1 = 25460

Expected output:

Lower_number = 25000
Upper_number = 26000

lower_number_differance= 460
upper_number_differance= 540


Tried code:

round(25460,-3) 

我得到的值是 25000,这是一个较低的数字。我需要所有其他值。

用于计算上限 1000th 值。您需要执行以下步骤:

  • 将一个值除以 1000(25460 将变为 25.460)
  • 然后ceil结果值(25.460会变成26)
  • 然后将结果乘以1000(26会变成26000)
import math

Number_1 = 25460

Lower_number = round(Number_1,-3)
print(Lower_number)
>> 25000

lower_number_differance = Number_1 - Lower_number
print(lower_number_differance)
>> 460

Upper_number  = int(math.ceil( Number_1/ 1000.0)) * 1000
print(Upper_number)
>> 26000

upper_number_differance = Upper_number -Number_1
print(upper_number_differance)
>> 540