Java Long Number Readable Format getTrafficString(long bytes)

Here you can find the source of getTrafficString(long bytes)

Description

get Traffic String

License

Open Source License

Declaration

public static String getTrafficString(long bytes) 

Method Source Code

//package com.java2s;
/*//  w  w  w .ja v  a2 s. c om
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * 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 {
    private static final long KILO = 1024;
    private static final long MEGA = KILO * KILO;
    private static final long GIGA = MEGA * KILO;
    private static final long TERA = GIGA * KILO;
    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(
            "#.##");

    public static String getTrafficString(long bytes) {
        if (bytes < KILO)
            return bytes + " bytes";
        if (bytes < MEGA)
            return DECIMAL_FORMAT.format((double) bytes / KILO)
                    + " kilobytes";
        if (bytes < GIGA)
            return DECIMAL_FORMAT.format((double) bytes / MEGA)
                    + " megabytes";
        if (bytes < TERA)
            return DECIMAL_FORMAT.format((double) bytes / GIGA)
                    + " gigabytes";

        return DECIMAL_FORMAT.format((double) bytes / TERA) + " terabytes";
    }
}

Related

  1. getHumanReadableSize(long bytes)
  2. getHumanReadableSize(long fileSize)
  3. getHumanReadableSize(long size, long unit, String unitName)
  4. getHumanSize(long size)
  5. getMaxHeap(String message)
  6. humanize(long value)
  7. humanizeBytes(long bytes)
  8. humanNumber(long num)
  9. humanreadable(long bytes, boolean si)