2012-09-13 5 views
2

아래의 모든 값은 double 형이지만 스위치에는 정수 값이 필요합니다. 이 주변에 어쨌든 있습니까?Double in 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; 
+3

예 -'if' /'else if' /'else'. – Mac

+0

평형에 대한 부동 소수점 비교는 절대로 좋은 생각이 아니므로 부동 소수점 위에 '전환'하는 것은 정말 나쁜 생각입니다. –

+1

'case' 값은 컴파일 타임 상수 여야합니다. –

답변

4

당신은 범위에 대한 다른 테스트 경우 사용하지만 당신은 당신 fivePercentValue에 대한 몇 가지 수학을 수행 한 후 다른 정수 예를 들어 다른 범위를 나타낼 수 있도록 정수로 변환 할 수의 가능성이 더 나은

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