2016-06-13 7 views
-1

in meiner app ich berechne die CPU-Auslastung und ich bekomme das Ergebnis in%, aber manchmal ist das Ergebnis zu groß 20.234234234234 Meine Frage ist, wie kann ich die Ergebnisnummern nach dem "."
Und kann ich dies tun, ohne Float in Ganzzahl zu ändern?Wie Dezimalstellen aus einem Float-Ergebnis zu entfernen?

Hier ist mein 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; 
} 

das Ergebnis drucken ich werfe einfach:

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

wo readCpuUsage() ist das Ergebnis und es% zu verwandeln i mit 100 multiplizieren Dies ist
, wie ich es tat:

float xxz=readCpuUsage()*100; 
    textview.setText(String.format("%.0f",xxz) + "%"); 
Diese 10
+0

Bitte bearbeiten und schreiben führen geben, wie Sie das Schwimmer wörtliche aktuell sind gedruckt wird. Ihre Funktion ist hier irrelevant, wenn es sich nur um eine Frage der Formatierung handelt. –

+0

Dezimalzahlen? Meinst du Dezimal * Orte *? – EJP

Antwort

2

Versuchen Sie es helfen kann Ihnen

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

oder

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

float xxz = readCpuUsage() * 100; textview.setText (String.format ("%. 0f", xxz) + "%"); –

0

Es gibt nur Langtyp in der folgenden Operation verwendet daher wird das Ergebnis immer lang sein.

zurück (float) (cpu2 - cpu1)/((cpu2 + idle2) - (cpu1 + idle1));

Ich verstehe nicht, warum Sie Typecasting es auf den ersten Platz zu schweben.

Ändern Sie einfach den Rückgabetyp von Float auf Long. Und die return-Anweisung ändern:

return (cpu2 - cpu1)/((cpu2 + Leerlauf2) - (cpu1 + Idle1));

+0

Das gibt mir das Ergebnis 0 –

1

Es sieht so aus, als ob Sie die Dinge unnötigerweise verkomplizieren.

Wenn Sie sicher sind, dass Sie die Dezimalkomponente Ihrer Nummer nicht möchten, sollten Sie einfach die Typen integer oder long verwenden. Es sei denn, Sie haben einen gültigen Grund oder eine spezielle Abweichung gegenüber dem int-Datentyp.

0
float x = 100.5; 
float result = x - (int)x; 
if (result != 0) 
{ 
    //If the value of `result` is not equal to zero, then, you have a decimal portion which is not equal to 0. 

//If you want integer result 
Integer yourAnswer1 = (Integer)result; 

Float yourAnswer2 = (float)result; 

} 
+0

float x = 100.5; es heißt: Benötigt float, gefunden double. –

+0

Entschuldigung, ersetzen Sie einfach Schwimmer x = 100,5; mit float x = 100.5f; –

0
float f=2443.4333; 
println(Math.floor(f)+""); 

dies werden Sie Ihre Frage 2443,0

+0

Danke Raut, aber ich möchte die Dezimalstellen vollständig entfernen. –

+0

Float wird immer die Dezimalzahl haben, besser ändern Sie es in die ganze Zahl – nlogn

+0

ok dann, wie kann ich das tun?denn wenn ich es in eine ganze Zahl ändere, ist das Ergebnis immer 0 –

Verwandte Themen