Trying to find a more elegant way to create an int that has discrete levels. My example only shows 6 different levels but I am wanting to do this for 45 different levels, so don’t want 45 if elses. Not sure what this is called in math and so can’t seem to find what I am looking for.
sd = some double value
int level = 0;
if (Double.compare(sd, 0.41) >= 0) {
level = 5;
} else if(Double.compare(sd, 0.25) >= 0) {
level = 4;
} else if(Double.compare(sd, 0.11) >= 0) {
level = 3;
} else if(Double.compare(sd, 0.05) >= 0) {
level = 2;
} else if(Double.compare(sd, 0.02) >= 0) {
level = 1;
}
…………….
Latest Update FYI these were the values I needed to quantize:
As you can see I needed something a little more elegant, Used Navigable Map answer
public static final int[] Levels = {
3100, 3250, 3383, 3517, 3650,
3673, 3695, 3718, 3740, 3760,
3780, 3800, 3820, 3853, 3885,
3918, 3950, 3975, 4000, 4025, 4050
};
http://download.oracle.com/javase/6/docs/api/java/util/NavigableMap.html
You can implement it with Double data type too.