Changeset a021e0d for core/java


Ignore:
Timestamp:
Feb 1, 2018 2:37:11 PM (3 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
42fa230
Parents:
81713a0
Message:

Console: Fix number formatting (tickets #1912, #1913, #2126)

Location:
core/java/src/net/i2p
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/DataHelper.java

    r81713a0 ra021e0d  
    15261526
    15271527    /**
    1528      * Caller should append 'B' or 'b' as appropriate
     1528     * This is binary, i.e. multiples of 1024.
     1529     * For decimal, see formatSize2Decimal().
     1530     *
     1531     * Caller should append 'B' or 'b' as appropriate.
     1532     *
     1533     * No space between the number and the letter.
    15291534     * NOTE: formatSize2() recommended in most cases for readability
     1535     *
     1536     * @return e.g. "123.05Ki"
    15301537     */
    15311538    public static String formatSize(long bytes) {
     
    15411548        String str = fmt.format(val);
    15421549        switch (scale) {
    1543             case 1: return str + "K";
    1544             case 2: return str + "M";
    1545             case 3: return str + "G";
    1546             case 4: return str + "T";
    1547             case 5: return str + "P";
    1548             case 6: return str + "E";
    1549             case 7: return str + "Z";
    1550             case 8: return str + "Y";
     1550            case 1: return str + "Ki";
     1551            case 2: return str + "Mi";
     1552            case 3: return str + "Gi";
     1553            case 4: return str + "Ti";
     1554            case 5: return str + "Pi";
     1555            case 6: return str + "Ei";
     1556            case 7: return str + "Zi";
     1557            case 8: return str + "Yi";
    15511558            default: return bytes + "";
    15521559        }
     
    15541561
    15551562    /**
     1563     * This is binary, i.e. multiples of 1024.
     1564     * For decimal, see formatSize2Decimal().
     1565     *
     1566     * Caller should append 'B' or 'b' as appropriate.
    15561567     * Like formatSize but with a non-breaking space after the number
    15571568     * This seems consistent with most style guides out there.
    15581569     * Use only in HTML, and not inside form values (use
    15591570     * formatSize2(bytes, false) there instead).
     1571     *
     1572     * @return e.g. "123.05&amp;#8239;Ki"
    15601573     * @since 0.7.14, uses thin non-breaking space since 0.9.31
    15611574     */
     
    15651578
    15661579    /**
     1580     * This is binary, i.e. multiples of 1024.
     1581     * For decimal, see formatSize2Decimal().
     1582     *
     1583     * Caller should append 'B' or 'b' as appropriate,
    15671584     * Like formatSize but with a space after the number
    15681585     * This seems consistent with most style guides out there.
     1586     *
    15691587     * @param nonBreaking use an HTML thin non-breaking space (&amp;#8239;)
     1588     * @return e.g. "123.05&amp;#8239;Ki" or "123.05 Ki"
    15701589     * @since 0.9.31
    15711590     */
     
    15821601        // Replace &nbsp; with thin non-breaking space &#8239; (more consistent/predictable width between fonts & point sizes)
    15831602
     1603        String space = nonBreaking ? "&#8239;" : " ";
     1604        String str = fmt.format(val) + space;
     1605        switch (scale) {
     1606            case 1: return str + "Ki";
     1607            case 2: return str + "Mi";
     1608            case 3: return str + "Gi";
     1609            case 4: return str + "Ti";
     1610            case 5: return str + "Pi";
     1611            case 6: return str + "Ei";
     1612            case 7: return str + "Zi";
     1613            case 8: return str + "Yi";
     1614            default: return bytes + space;
     1615        }
     1616    }
     1617
     1618    /**
     1619     * This is decimal, i.e. multiples of 1000.
     1620     * For binary, see formatSize2().
     1621     *
     1622     * Caller should append 'B' or 'b' as appropriate.
     1623     * Like formatSize but with a space after the number
     1624     * This seems consistent with most style guides out there.
     1625     *
     1626     * @return e.g. "123.05&amp;#8239;K"
     1627     * @since 0.9.34
     1628     */
     1629    public static String formatSize2Decimal(long bytes) {
     1630        return formatSize2Decimal(bytes, true);
     1631    }
     1632
     1633    /**
     1634     * This is decimal, i.e. multiples of 1000.
     1635     * For binary, see formatSize2().
     1636     *
     1637     * Caller should append 'B' or 'b' as appropriate.
     1638     * Like formatSize but with a space after the number
     1639     * This seems consistent with most style guides out there.
     1640     *
     1641     * @param nonBreaking use an HTML thin non-breaking space (&amp;#8239;)
     1642     * @return e.g. "123.05&amp;#8239;K" or "123.05 K"
     1643     * @since 0.9.34
     1644     */
     1645    public static String formatSize2Decimal(long bytes, boolean nonBreaking) {
     1646        double val = bytes;
     1647        int scale = 0;
     1648        while (val >= 1000) {
     1649            scale++;
     1650            val /= 1000;
     1651        }
     1652        DecimalFormat fmt = new DecimalFormat("##0.00");
    15841653        String space = nonBreaking ? "&#8239;" : " ";
    15851654        String str = fmt.format(val) + space;
  • core/java/src/net/i2p/util/LogManager.java

    r81713a0 ra021e0d  
    553553            if (v.length() < 2)
    554554                return -1;
    555             if (v.endsWith("B"))
     555            if (v.endsWith("IB"))
     556                v = v.substring(0, v.length() - 2);
     557            else if (v.endsWith("B"))
    556558                v = v.substring(0, v.length() - 1);
    557559            char mod = v.charAt(v.length() - 1);
Note: See TracChangeset for help on using the changeset viewer.