I need values that go like this:
0.5 1 1.5 2 2.5 3 Etc
I made a function but I don't know if it can be done in another way that requires less code, here is my code:
private String GetNumber(int spinner) {
String result = "0";
if (spinner == 1) {
result = "0.5";
}
if (spinner == 2) {
result = "1";
}
if (spinner == 3) {
result = "1.5";
}
if (spinner == 4) {
result = "2";
}
if (spinner == 5) {
result = "2.5";
}
if (spinner == 6) {
result = "3";
}
if (spinner == 7) {
result = "3.5";
}
if (spinner == 8) {
result = "4";
}
if (spinner == 9) {
result = "4.5";
}
if (spinner == 10) {
result = "5";
}
if (spinner == 11) {
result = "5.5";
}
if (spinner == 12) {
result = "6";
}
if (spinner == 13) {
result = "6.5";
}
if (spinner == 14) {
result = "7";
}
if (spinner == 15) {
result = "7.5";
}
if (spinner == 16) {
result = "8";
}
if (spinner == 17) {
result = "8.5";
}
if (spinner == 18) {
result = "9";
}
if (spinner == 19) {
result = "9.5";
}
if (spinner == 20) {
result = "10";
}
return result;
}
Is it the correct way or is there an easier way?
It could simplify the function that returns half of the parameter, but within it it would be validated if it is an even integer
num%2==0
, if so, the result would be an integer. otherwise the division by2.0
is the most important part that will convert the result into afloat
if necessary.In case you want to fill the
Spinner
with elements starting with0.5
, it would suffice to add a simple type to start and increment with said value, the function will have the limit value of the elements as a parameter.0.5
0.5
for
double
The only thing I can think of is to use a switch:
The sequence returns half of the number that is entered.