I have the following function which gets an integer (from a database but I have checked that the number is sent properly in the function). I want to transform to string and return it in a specific way depending how large the number is.
For some weird reason it works fine with numbers < 10000, but from that point on it just shows weird results
Function is:
public static String numberTransformation(int number){
String str=String.valueOf(number);
System.out.println("#################### NUMBER BEFORE TRANSFORMATION" + str);
if(number<=999){
return str;
}
else if(number>999 && number<1099){
return "1k";
}
else if(number>1099 && number<=9999){
return str.charAt(0) + "." + str.charAt(1) + "k";
}
else if (number>9999 && number<=99999){
return str.charAt(0) + str.charAt(1) + "k";
}
else if (number>99999 && number<=999999){
return str.charAt(0) + str.charAt(1) + str.charAt(2) + "k";
}
return "";
}
Some tests and results:
Number is 999 -> result is 999 (expected)
Number is 2532 -> result is 2.5k (expected)
Number is 10000 -> result is 97k (not expected)
Number is 100000 -> result is 145k (not expected)
What did I do wrong? Does it have something to do with the number being larger than 10000?