Java Byte Array Shorten abbrevBytes(long bytes)

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

Description

abbrev Bytes

License

Educational Community License

Return

a friendly looking string to represent the given number of bytes: e.g.: 120k, or 3.8M. for values less than zero, returns ""

Declaration


public static String abbrevBytes(long bytes) 

Method Source Code

//package com.java2s;
/*/*from w  w  w.  jav a2  s  .  c om*/
* Copyright 2003-2010 Tufts University  Licensed under the
 * Educational Community 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.osedu.org/licenses/ECL-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.
 */

public class Main {
    /** @return a friendly looking string to represent the given number of bytes: e.g.: 120k, or 3.8M.
     * for values less than zero, returns ""
     */

    public static String abbrevBytes(long bytes) {
        if (bytes > 1024 * 1024)
            return String.format("%.1fM", (bytes / (1024.0 * 1024)));
        else if (bytes > 1024)
            return bytes / 1024 + "k";
        else if (bytes >= 0)
            return "" + bytes;
        else
            return "";
    }
}

Related

  1. abbreviate(byte[] bytes, int offset, int maxLength)
  2. abbreviate(final byte[] bytes)