Java Size getFormatSize(long size)

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

Description

get Format Size

License

Apache License

Declaration

public static String getFormatSize(long size) 

Method Source Code


//package com.java2s;
/*//from w  ww  .j a v a  2s.co  m
 * Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences.
 * 
 * This file is part of Duckling project.
 *
 * 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 {
    public final static double KB_SCALE = 1024;
    public final static double MB_SCALE = 1024 * 1024;
    public final static double GB_SCALE = 1024 * 1024 * 1024;
    private static DecimalFormat df = new DecimalFormat("#.00");

    public static String getFormatSize(long size) {
        double d = size;
        if (size < KB_SCALE) {
            return size + " B";
        }
        if (size < MB_SCALE) {
            return df.format(d / KB_SCALE) + " KB";
        }
        if (size < GB_SCALE) {
            return df.format(d / MB_SCALE) + " MB";
        }
        return df.format(d / GB_SCALE) + " GB";
    }
}

Related

  1. getBytesSpecificationDescription(double sizeInBytes, String formatSpec)
  2. getByteStr(long size)
  3. getDataSize(long size)
  4. getFontSizeInPoints(String fontSizeWithUnit)
  5. getFormatSize(double size)
  6. getFormattedSize(long size)
  7. getReadableByteSize(long size)
  8. getReadableSize(long size)
  9. getSize(long octets)