1

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

  • android studio is an IDE, meaning it just helps you to code. please don't use the tag unless you're specifically asking about a feature of the IDE – a_local_nobody Jan 03 '21 at 15:42
  • If you also need rounding, see https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Hulk Jan 03 '21 at 15:59

2 Answers2

0

Integer.parseInt convert String into int.
if you divide an int by int the result will be int too.
use Float.parseFloat() instead, or divide it by 1000.0 (make the 1000 non-integer value and then the result will be non-integer too.)
if you want it to be more precise and count milligrams (or even smaller units) you must use Float.parseFloat() because the value after the dot will be discarded using Integer.parseInt().

ATP
  • 2,939
  • 4
  • 13
  • 34
0

Here you go, I wrote a java code for this and I am sure it is going to work in android as well.

package stackOverflow;

import java.util.*;
import java.io.*;
public class parseWeight {

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   

        //Ignore above


        //assuming you have int as input
        int weightInput = Integer.parseInt(br.readLine());

        String outputString = "";
        if(weightInput < 1000){
            outputString = weightInput + " gram";
        }
        else if(weightInput >= 1000){
            double temp = weightInput / 1000.0;


            //round off upto one decimal places
            double rf = Math.round((temp*10.0)/10.0);
            outputString = rf + " kg";
        }


        tvWeight.setText(": " + outputString);


        //Ignore below
        System.out.println(outputString);

    }
    
}
koshur
  • 124
  • 1
  • 9