Java Formatter Usage toReadableSize(long bytes)

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

Description

to Readable Size

License

Apache License

Declaration

public static String toReadableSize(long bytes) 

Method Source Code

//package com.java2s;
// Licensed to the Apache Software Foundation (ASF) under one

import java.util.Formatter;
import java.util.Locale;

public class Main {
    protected static final long KB = 1024;
    protected static final long MB = 1024 * KB;
    protected static final long GB = 1024 * MB;
    protected static final long TB = 1024 * GB;

    public static String toReadableSize(long bytes) {
        if (bytes < KB && bytes >= 0) {
            return Long.toString(bytes) + " bytes";
        }/*ww w .j av a2s.  com*/
        StringBuilder builder = new StringBuilder();
        Formatter format = new Formatter(builder, Locale.getDefault());
        if (bytes < MB) {
            format.format("%.2f KB", (float) bytes / (float) KB);
        } else if (bytes < GB) {
            format.format("%.2f MB", (float) bytes / (float) MB);
        } else if (bytes < TB) {
            format.format("%.2f GB", (float) bytes / (float) GB);
        } else {
            format.format("%.4f TB", (float) bytes / (float) TB);
        }
        format.close();
        return builder.toString();
    }
}

Related

  1. msToHumanReadableDelta(long start)
  2. numberWithLeadingZeroes(int n, int totalChars)
  3. showVxlanHeaderOutput()
  4. substitution(Formatter formatter, int flags, int width, int precision, StringBuilder obj)
  5. toHexString(final byte[] data)
  6. toTwoDigit(double f)
  7. toUUIDFormat(byte[] bytes)
  8. validateCondition(boolean condition, String messageFormat, Object... messageArgs)
  9. warnfErr(String format, String... params)