Java File Size Readable Format FormatSize(long size)

Here you can find the source of FormatSize(long size)

Description

Format Size

License

Open Source License

Declaration

public static String FormatSize(long size) 

Method Source Code

//package com.java2s;
/**//ww w . j a  v a 2s .c  o m
 * KTH Developed by Java
 *
 * @Copyright 2011 by Service Platform Development Team, KTH, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of KTH, Inc.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with KTH.
 */

public class Main {
    public static String FormatSize(long size) {
        String suffix = null;
        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                size /= 1024;
            }
        }
        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }
        if (suffix != null) {
            resultBuffer.append(suffix);
        }
        return resultBuffer.toString();
    }
}

Related

  1. formatSize(long size)
  2. formatSize(long size)
  3. formatSize(long size)
  4. formatSize(Long size)
  5. formatSize(long size)
  6. formatSize(long v)
  7. formatSize(String strSize)
  8. formatSize(StringBuilder builder, Long size)
  9. formatSizeInBytes(int size)