Merge branch 'material_toolbar' of https://github.com/owncloud/android into material_...
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / DisplayUtils.java
index 3ae36e4..236a05f 100644 (file)
@@ -22,6 +22,7 @@
 \r
 package com.owncloud.android.utils;\r
 \r
+import java.math.BigDecimal;\r
 import java.net.IDN;\r
 import java.text.DateFormat;\r
 import java.util.Arrays;\r
@@ -59,6 +60,7 @@ public class DisplayUtils {
     //private static String TAG = DisplayUtils.class.getSimpleName(); \r
     \r
     private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };\r
+    private static final int[] sizeScales = { 0, 0, 0, 1, 1, 2, 2, 2, 2 };\r
 \r
     private static HashMap<String, String> mimeType2HUmanReadable;\r
     static {\r
@@ -118,7 +120,11 @@ public class DisplayUtils {
     \r
     /**\r
      * Converts the file size in bytes to human readable output.\r
-     * \r
+     * <ul>\r
+     *     <li>appends a size suffix, e.g. B, KB, MB etc.</li>\r
+     *     <li>rounds the size based on the suffix to 0,1 or 2 decimals</li>\r
+     * </ul>\r
+     *\r
      * @param bytes Input file size\r
      * @return Like something readable like "12 MB"\r
      */\r
@@ -129,8 +135,9 @@ public class DisplayUtils {
             result /= 1024.;\r
             attachedsuff++;\r
         }\r
-        result = ((int) (result * 100)) / 100.;\r
-        return result + " " + sizeSuffixes[attachedsuff];\r
+\r
+        return new BigDecimal(result).setScale(\r
+                sizeScales[attachedsuff], BigDecimal.ROUND_HALF_UP) + " " + sizeSuffixes[attachedsuff];\r
     }\r
 \r
     /**\r