Java Size byteCountToDisplaySize(long size)

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

Description

Get the display of the given byte size.

License

Apache License

Parameter

Parameter Description
size size

Return

display version of the byte size

Declaration

public static String byteCountToDisplaySize(long size) 

Method Source Code


//package com.java2s;
/* //w  w w  . j a  v  a2 s  .c  om
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

import java.text.DecimalFormat;

public class Main {
    /**
     * The number of bytes in a kilobyte.
     */
    public static final long ONE_KB = 1024;
    /**
     * The number of bytes in a megabyte.
     */
    public static final long ONE_MB = ONE_KB * ONE_KB;
    /**
     * The number of bytes in a gigabyte.
     */
    public static final long ONE_GB = ONE_KB * ONE_MB;

    /**
     * Get the display of the given byte size.
     * 
     * @param size
     *            size
     * @return display version of the byte size
     */
    public static String byteCountToDisplaySize(long size) {
        String displaySize;
        DecimalFormat format = new DecimalFormat("###0.0");
        if (size / ONE_GB > 0) {
            displaySize = format.format((double) size / ONE_GB) + "GB";
        } else if (size / ONE_MB > 0) {
            displaySize = format.format((double) size / ONE_MB) + "MB";
        } else if (size / ONE_KB > 0) {
            displaySize = format.format((double) size / ONE_KB) + "KB";
        } else {
            displaySize = String.valueOf(size) + "B";
        }
        return displaySize;
    }
}

Related

  1. byteCountToDisplaySize(long bytes)
  2. byteCountToDisplaySize(long size)
  3. byteCountToDisplaySize(long size)
  4. byteSizeString(long bytes)
  5. BytesToHRSize(String len)
  6. bytesToSizeFormat(long bytes)
  7. bytesToString(long sizeInBytes)