How to remove decimals from a float result?

14,415

Solution 1

Try This it may be help to you

String.format("%.0f",floatvalue);

or

Float.parseFloat(String.format("%.0f",floatvalue))

Solution 2

It looks like you are unnecessarily complicating things.

When you know for sure that you do not want the decimal component of your number you should just use the integer or long types. Unless you have a valid reason or specific bias against the int datatype.

Share:
14,415
George Lungu
Author by

George Lungu

Updated on June 04, 2022

Comments

  • George Lungu
    George Lungu almost 2 years

    in my app i calculate the CPU usage and i get the result in % but sometimes the result is too big 20.234234234234 My question is, how can i completely remove the result numbers after the "."?
    And can i do this without changing float to integer?

    Here is my code:

        private float readCpuUsage() {
        try {
            RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
            String load = reader.readLine();
    
            String[] toks = load.split(" ");
    
            long idle1 = Long.parseLong(toks[5]);
            long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3])
                    + Long.parseLong(toks[4]) + Long.parseLong(toks[6])
                    + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
    
            try {
                Thread.sleep(360);
            } catch (Exception e) {
            }
    
            reader.seek(0);
            load = reader.readLine();
            reader.close();
    
            toks = load.split(" ");
    
            long idle2 = Long.parseLong(toks[5]);
            long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3])
                    + Long.parseLong(toks[4]) + Long.parseLong(toks[6])
                    + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
    
            return (float) (cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
    
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    
        return 0;
    }
    

    To print the result i just throw:

          textview.setText((readCpuUsage() * 100) + "%");
    

    where readCpuUsage() is the result and to transform it to % i multiply it with 100
    This is how i did it:

        float xxz=readCpuUsage()*100;
        textview.setText(String.format("%.0f",xxz) + "%");
    
  • George Lungu
    George Lungu almost 8 years
    Thanks Raut, but i want to remove completely the decimals.
  • George Lungu
    George Lungu almost 8 years
    This gives me the result 0
  • George Lungu
    George Lungu almost 8 years
    float x = 100.5; it says: Required float, found double.
  • nlogn
    nlogn almost 8 years
    float will always have the decimal, better change it to integer
  • George Lungu
    George Lungu almost 8 years
    ok then, how can i do this? because when i change it to integer, the result is always 0
  • Er. Kaushik Kajavadara
    Er. Kaushik Kajavadara almost 8 years
    sorry, just replace float x = 100.5; with float x = 100.5f;
  • George Lungu
    George Lungu almost 8 years
    float xxz=readCpuUsage()*100; textview.setText(String.format("%.0f",xxz) + "%");
  • nlogn
    nlogn almost 8 years
    float flt=20.234234234234; println(((int)Math.floor(flt))+""); It gives result 20