在Switch语句中使用Double

Ast*_*en2 3 objective-c switch-statement ios

下面的所有值都是双精度数,但是switch需要一个整数值.有没有办法解决?

switch(fivePercentValue){
case floor((5*fivePercentValue) / 100):
    fivePercent_.backgroundColor = [UIColor greenColor];
    fivePercentLabel_.textColor = [UIColor greenColor];
    break;
case ceil((5*fivePercentValue) / 100):
    fivePercent_.backgroundColor = [UIColor greenColor];
    fivePercentLabel_.textColor = [UIColor greenColor];
    break;
default:
    fivePercent_.backgroundColor = [UIColor redColor];
    fivePercentLabel_.textColor = [UIColor redColor];
    break;
Run Code Online (Sandbox Code Playgroud)

Nat*_*Day 6

你可能只是使用if else并测试范围,但是你可以对你的五个值进行一些数学运算,然后将它转换为一个整数,这样不同的整数代表不同的范围,例如

switch( (int)(value*10.0) )
{
    case 0:        // this is 0.0 <= value < 0.1
        break;
    case 1:        // this is 0.1 <= value < 0.2
        break;
    case 2:        // this is 0.2 <= value < 0.3
        break;
    ....
}
Run Code Online (Sandbox Code Playgroud)