Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Double in Switch Statement

All of the values below are doubles, yet switch requires a integer value. Is there anyway around this?

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;
like image 998
Astephen2 Avatar asked Sep 13 '25 01:09

Astephen2


1 Answers

You are probable better of just using if else and testing for ranges but you can perform some maths on your fivePercentValue and then convert it to an integer so that different integers represent different ranges for example

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;
    ....
}
like image 77
Nathan Day Avatar answered Sep 14 '25 15:09

Nathan Day