在Switch语句中使用Double

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

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; 

你可能会更好地使用if else和testing范围,但你可以在你的fivePercentValue上执行一些math,然后将其转换为一个整数,以便不同的整数代表不同的范围,例如

 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; .... }