Java tutorial
//package com.java2s; import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static String formatByte(long l) { String s = "0 KB"; if (l <= 0x100000L) { BigDecimal bigdecimal = new BigDecimal(l); BigDecimal bigdecimal1 = new BigDecimal(1024); RoundingMode roundingmode = RoundingMode.CEILING; String s1 = String.valueOf(bigdecimal.divide(bigdecimal1, 0, roundingmode).toString()); s = (new StringBuilder(s1)).append(" KB").toString(); } else { if (l >= 0x40000000L) { BigDecimal bigdecimal2 = new BigDecimal(l); BigDecimal bigdecimal3 = new BigDecimal(0x40000000); RoundingMode roundingmode1 = RoundingMode.CEILING; String s2 = String.valueOf(bigdecimal2.divide(bigdecimal3, 2, roundingmode1).toString()); s = (new StringBuilder(s2)).append(" G").toString(); } else { BigDecimal bigdecimal4 = new BigDecimal(l); BigDecimal bigdecimal5 = new BigDecimal(0x100000); RoundingMode roundingmode2 = RoundingMode.CEILING; String s3 = String.valueOf(bigdecimal4.divide(bigdecimal5, 2, roundingmode2).toString()); s = (new StringBuilder(s3)).append(" MB").toString(); } } return s; } }