Java Long Number Readable Format toKilobytes(long bytes)

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

Description

This static method converts the passed in number of bytes into a kilobyte string grouping digits with locale-dependant thousand separator and with "KB" locale-dependant unit at the end.

License

Open Source License

Parameter

Parameter Description
bytes the number of bytes to convert to a kilobyte String.

Return

a String representing the number of kilobytes that the bytes argument evaluates to, with "KB" appended at the end. If the input value is negative, the string returned will be "? KB".

Declaration

public static String toKilobytes(long bytes) 

Method Source Code

//package com.java2s;
/*//from ww w  .j  av a2s .  c o m
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.NumberFormat;

public class Main {
    /**
     * Localizable Number Format constant for the current default locale
     * set at init time.
     */
    private static NumberFormat NUMBER_FORMAT0;
    /**
     * Localizable constants
     */
    public static String GENERAL_UNIT_KILOBYTES;

    /**
     * This static method converts the passed in number of bytes into a
     * kilobyte string grouping digits with locale-dependant thousand separator
     * and with "KB" locale-dependant unit at the end.
     *
     * @param bytes the number of bytes to convert to a kilobyte String.
     *
     * @return a String representing the number of kilobytes that the
     *         <code>bytes</code> argument evaluates to, with "KB" appended
     *         at the end.  If the input value is negative, the string
     *         returned will be "? KB".
     */
    public static String toKilobytes(long bytes) {
        if (bytes < 0)
            return "? " + GENERAL_UNIT_KILOBYTES;
        long kbytes = bytes / 1024;
        // round to nearest multiple, or round up if size below 1024
        if ((bytes & 512) != 0 || (bytes > 0 && bytes < 1024))
            kbytes++;
        // result formating, according to the current locale
        return NUMBER_FORMAT0.format(kbytes) + GENERAL_UNIT_KILOBYTES;
    }
}

Related

  1. stringify(long byteNumber)
  2. toHuman(long amount)
  3. toHumanReadableSize(long byteCount)
  4. toHumanReadableSize(long bytes)
  5. toHumanSize(long bytes)
  6. toLocalizedInteger(long value)
  7. toMB(long b)
  8. toMB(long bytes)
  9. toReadableBytes(long bytes)