I'd like to format following numbers into the numbers next to them with Android:
I've tried it, by taking the code from How to go about formatting 1200 to 1.2k in Android studio
This works when the value is a multiple of zero, but if there is a number other than zero some are not suitable
String numberString = "";
if (Math.abs(Integer.parseInt(weight_total) / 1000) > 1) {
numberString = (Integer.parseInt(weight_total) / 1000) + " kg";
}
else {
numberString = weight_total + " gram";
}
tvWeight.setText(": " + numberString);
I want to 1000 gram > 1 kg, 1800 gram > 1.8 kg etc
Correct and Wrong weight screenshot https://i.stack.imgur.com/mqA0x.jpg
Now i am using this code, so its work fine and perfect for my app
// Input data
int weightInput = Integer.parseInt(item.getWeight());
String weightOutput;
if (weightInput < 1000) {
weightOutput = weightInput + " gram";
} else {
double result = weightInput / 1000.0;
weightOutput = String.valueOf(result);
weightOutput = (weightOutput.contains(".0") ? weightOutput.substring(0, weightOutput.length() - 2) : weightOutput) + " kg";
}
System.out.println(weightOutput);
Final result https://i.stack.imgur.com/1Nxbs.png