Ios 检查浮点值

Ios 检查浮点值,ios,swift,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

我在做一个小游戏,如果你得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

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

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