Java Fraction Format formatBytes(final double bytes)

Here you can find the source of formatBytes(final double bytes)

Description

Formats the given number of bytes into an easily readable format

License

Open Source License

Parameter

Parameter Description
bytes The number of bytes to format

Return

String containing a formatted version of the input data

Declaration

public static String formatBytes(final double bytes) 

Method Source Code


//package com.java2s;
/*//from  w  ww  .j av  a  2  s .c  o m
 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:  Definitions for the class GraphUtils
 *
 */

import java.text.DecimalFormat;

public class Main {
    private static final DecimalFormat MB_FORMAT = new DecimalFormat("#####.0");
    private static final DecimalFormat BYTES_FORMAT = new DecimalFormat("#####.##");

    /**
     * Formats the given number of bytes into an easily readable format
     * 
     * @param bytes
     *            The number of bytes to format
     * @return String containing a formatted version of the input data
     */
    public static String formatBytes(final double bytes) {
        String scaledY;

        if (bytes < 10000) {
            scaledY = BYTES_FORMAT.format((long) bytes) + " B";
        } else if (bytes <= 500 * 1024) {
            scaledY = BYTES_FORMAT.format(bytes / 1024) + " KB";
        } else {
            scaledY = MB_FORMAT.format(((float) bytes / (1024 * 1024))) + " MB";
        }
        return scaledY;
    }
}

Related

  1. formatAnotherString(double valor)
  2. formataNum(double numero)
  3. formatAvgTime(double avg)
  4. formatBet(Double betOdd, int formatterDigits)
  5. formatBetHandicap(Double handicap)
  6. formatCoordinates(double lat, double lon)
  7. formatCPUUtilization(double d)
  8. formatD(double d)
  9. formatData(double aAmount)