检查浮点值

时间:2019-01-03 23:45:31

标签: ios swift

我正在做一个小游戏,如果您的得分为34,则意味着您获得3乘以2等于6;如果您的得分为36,则意味着您获得了4 * 2 = 8,但是如果您获得35,则它将相同只需3.5 * 2 = 7

这是一个小代码,我可以检查您是6或以上还是4或以下,但我无法解决5的

// example:
// player score 35 he will get 7 because 3.5 * 2 = 7
// player score 37 he will get 8 because 4 * 2 = 8
// player score 32 he will get 6 because 3 * 2 = 6



var newScore = Double(score)
newScore = newScore / 10
newScore = round(newScore)
newScore = newScore * 2

// I am not sure how to check if the user gets 35 or 45 
// first I have to convert it to float so it will be 3.5 or 4.5
// then check if it has the .5 otherwise the round function will round it to 4 or 5 instead of * it by 2

1 个答案:

答案 0 :(得分:-1)

也许还有其他方法,但是对于您的情况,您可以这样做

var newScore = Double(score) 
newScore = newScore / 10 
if score % 10 != 5 {  
    newScore = round(newScore)  
} 
print(newScore) 
print(newScore * 2)