2012-03-19 8 views

risposta

44

Adattare la risposta da oltre here esso dovrebbe essere simile

public static String withSuffix(long count) { 
    if (count < 1000) return "" + count; 
    int exp = (int) (Math.log(count)/Math.log(1000)); 
    return String.format("%.1f %c", 
         count/Math.pow(1000, exp), 
         "kMGTPE".charAt(exp-1)); 
} 

Codice di prova:

for (long num : new long[] { 0, 27, 999, 1000, 110592, 
          28991029248L, 9223372036854775807L }) 
    System.out.printf("%20d: %8s%n", num, withSuffix(num)); 

uscita:

    0:  0 
        27:  27 
       999:  999 
       1000: 1.0 k 
       110592: 110.6 k 
     28991029248: 29.0 G 
9223372036854775807: 9.2 E 
+1

Questo è scritto così bello! Grazie. – StackPWRequirmentsAreCrazy

+0

come può fare 1.0k a 1k se il decimale è 0? –

+1

Questa risposta è semplicemente bellissima. – Sotti

Problemi correlati