File: bytes.md

package info (click to toggle)
bnd 5.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,128 kB
  • sloc: java: 249,039; xml: 90,728; sh: 655; perl: 153; makefile: 96; python: 47; javascript: 9
file content (30 lines) | stat: -rw-r--r-- 825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
---
layout: default
class: Macro
title: bytes ( ';' LONG )*
summary: Format bytes
---

    /**
     * Format bytes
     */
    public String _bytes(String[] args) {
        try (Formatter sb = new Formatter()) {
            for (int i = 0; i < args.length; i++) {
                long l = Long.parseLong(args[1]);
                bytes(sb, l, 0, new String[] {
                    "b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb", "Bb", "Geopbyte"
                });
            }
            return sb.toString();
        }
    }

    private void bytes(Formatter sb, double l, int i, String[] strings) {
        if (l > 1024 && i < strings.length - 1) {
            bytes(sb, l / 1024, i + 1, strings);
            return;
        }
        l = Math.round(l * 10) / 10;
        sb.format("%s %s", l, strings[i]);
    }