Java Fraction Format formatData(double aAmount)

Here you can find the source of formatData(double aAmount)

Description

Format the amount of data in bytes to a nice readable string

License

Apache License

Parameter

Parameter Description
aAmount the amount in bytes

Return

a nice data size string

Declaration

public static String formatData(double aAmount) 

Method Source Code

//package com.java2s;
/*//from   w  w  w .j ava 2  s.  co m
 *  Copyright 2006 The National Library of New Zealand
 *
 *  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 {
    /**
     * Format the amount of data in bytes to a nice readable string
     * @param aAmount the amount in bytes
     * @return a nice data size string
     */
    public static String formatData(double aAmount) {
        DecimalFormat df = new DecimalFormat("#,###,###.##");
        String type = "";
        double niceDataDown = 0;
        double dataDown = aAmount;

        if (dataDown < 1024) {
            niceDataDown = dataDown;
            type = " bytes";
        } else if ((dataDown / 1024) < 1024) {
            niceDataDown = dataDown / 1024;
            type = " KB";
        } else if ((dataDown / (1024 * 1024)) < 1024) {
            niceDataDown = dataDown / (1024 * 1024);
            type = " MB";
        } else if ((dataDown / (1024 * 1024 * 1024)) < 1024) {
            niceDataDown = dataDown / (1024 * 1024 * 1024);
            type = " GB";
        }

        return df.format(niceDataDown) + type;
    }
}

Related

  1. formatBetHandicap(Double handicap)
  2. formatBytes(final double bytes)
  3. formatCoordinates(double lat, double lon)
  4. formatCPUUtilization(double d)
  5. formatD(double d)
  6. formatDataValue(double value)
  7. formatDecimal(double dtSource, String nbit)
  8. formatDecimal(double number)
  9. formatDecimal(double numToFormat)