Java Size Format formatFileSize(long size)

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

Description

formatFileSize Convert a int representation into the label of file size.

License

Open Source License

Parameter

Parameter Description
intValue Int value representation

Return

String The String format after convertion

Declaration

public static String formatFileSize(long size) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .j a v  a  2  s  . c om*/
 * @(#)TextUtility.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  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 DCIVision Ltd.
 */

import java.text.NumberFormat;

public class Main {
    /**
     * formatFileSize
     *
     * Convert a int representation into the label of file size.
     *
     * @param    intValue    Int value representation
     * @return   String      The String format after convertion
     */
    public static String formatFileSize(long size) {
        StringBuffer str = new StringBuffer();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(1);
        nf.setMinimumFractionDigits(1);

        if (size <= 0) {
            return "0 bytes";
        }

        if (size < 1024) {
            str.append(size).append(" bytes");
        } else if (size < 1048576) {
            str.append(nf.format(size / 1024.0)).append(" KB");
        } else if (size < 1073741824) {
            str.append(nf.format((size / 1024.0) / 1024.0)).append(" MB");
        } else {
            str.append(nf.format(size / (1024.0 * 1024 * 1024))).append(" GB");
        }

        return str.toString();
    }
}

Related

  1. formatFileSize(long length)
  2. formatFileSize(long size)
  3. formatFileSize(long size)
  4. formatFileSize(long size)
  5. formatFileSize(long size)
  6. formatFileSize(Long sizeBytes)
  7. formatFilesizeGB(long filesize, int fractionDigits)
  8. formatGameSize(int size)
  9. formatMemorySize(long size)