This is a simple Java question. I don’t know what to search for in stackoverflow to get to the right question. My requirement is very simple :
Given a double value which lies between 0 and 10, convert it to an integer using the following logic.
If the double value is >=0.0D && <0.5D output=1
If the double value is >=5.0D && <1.0D output=2
If the double value is >=1.0D && <1.5D output=3
....................so on up till
If the double value is >=9.5D && <10.0D output=20
Preferred to extend the range well beyond 10.0D later on.
Don’t want to keep on writing case and if else conditions.
How to achieve this efficiently ?
Not sure how I can use Math.ceil or Math.floor to achieve this.
public int convert(double d)
{
//logic to go here
}
Any help is appreciated. Ok to use any collections or other apis for this.
Also how to change the logic later on if the increments are not in 0.5 and want it to be variable.