简体   繁体   English

python将多余的float保存到int转换

[英]python saving the excess of a float to int conversion

x=10.5
    if x==10.5:
        x=int(x)+1
        y= .5

ok i have x=10.5 i want to round up to 11 but say the .5 to use later is there any way to do this when i dont know what x will be all the time? 好吧,我有x = 10.5我想要舍入到11但是说.5以后使用是否有任何方法可以做到这一点,当我不知道x将一直是什么时候?

i have to real place to start or even if its possible i do know how to change it to an int but i want to store what ever it took off and store to y right now id have to write 100 of if statments to figure what do say and that just doent strike me as the best way to do it 我必须真正的地方开始,或者即使它可能我知道如何将其更改为int但我想存储它起飞和存储到y现在id必须写100个if if statments来计算什么做说而且只是让我觉得这是最好的方法

One fell swoop: 一举一动:

>>> divmod(10.5,1)
(10.0, 0.5)

The docs for divmod can be found here . divmod的文档可以在这里找到。

x = 10.5
x,chopped = int(x), x - int(x)

take the number modulus 1 取数模1

>>> 10.5%1
0.5

This question is rather poorly worded. 这个问题的措辞很差。 Is this what you are trying to do? 这是你想要做的吗?

x = 10.5

y = math.fmod(x, 1.0) # y = 0.5
x = math.ceil(x) # x = 11.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM