You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Mark (JIRA)" <ji...@apache.org> on 2013/03/25 00:05:17 UTC

[jira] [Issue Comment Deleted] (IO-226) Rounding issue with byteCountToDisplaySize(long size)

     [ https://issues.apache.org/jira/browse/IO-226?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Mark updated IO-226:
--------------------

    Comment: was deleted

(was: Here is a variant that is able to let the user specify the maximum number of characters used to display the digits part of the displayed string. The rounding also makes a lot of sense to me.

{code}
public class FileUtils {

    private static final int DEFAULT_MAXCHARS = 3;
    private static final BigDecimal KILO_DIVISOR = new BigDecimal(1024L);

    enum SizeSuffix {

        B, KB, MB, GB, TB, PB, EB;
    }

    /**
     * Adopted and improved version of
     * {@link org.apache.commons.io.FileUtils#byteCountToDisplaySize(BigInteger)}.
     *
     * @see https://issues.apache.org/jira/browse/IO-226 - should the rounding be changed?
     * @param size
     * @param maxChars maximum length of digit part, ie. '1.2'
     * @return rounded byte size as {@link java.lang.String}
     */
    public static String byteCountToDisplaySize(BigInteger size, int maxChars) {
        String displaySize;
        BigDecimal bdSize = new BigDecimal(size);
        SizeSuffix selectedSuffix = SizeSuffix.B;
        for (SizeSuffix sizeSuffix : SizeSuffix.values()) {
            selectedSuffix = sizeSuffix;
            if (bdSize.setScale(0, RoundingMode.HALF_UP).toString().length() <= maxChars) {
                break;
            }
            bdSize = bdSize.divide(KILO_DIVISOR);
        }
        displaySize = bdSize.setScale(0, RoundingMode.HALF_UP).toString();
        if (displaySize.length() < maxChars - 1) {
            displaySize = bdSize.setScale(
                    maxChars - 1 - displaySize.length(), RoundingMode.HALF_UP).toString();
        }
        return displaySize + " " + selectedSuffix.toString();
    }

    public static String byteCountToDisplaySize(BigInteger size) {
        return byteCountToDisplaySize(size, DEFAULT_MAXCHARS);
    }

    public static String byteCountToDisplaySize(long size, int maxChars) {
        return byteCountToDisplaySize(BigInteger.valueOf(size), maxChars);
    }

    public static String byteCountToDisplaySize(long size) {
        return byteCountToDisplaySize(BigInteger.valueOf(size), DEFAULT_MAXCHARS);
    }
}
{code}

{code}
// CHECKSTYLE IGNORE MagicNumber FOR NEXT 1000 LINES
public class FileUtilsTest {

    /**
     * Test of byteCountToDisplaySize method, of class FileUtils.
     */
    @Test
    public void testByteCountToDisplaySize() {
        assertEquals("999 B", FileUtils.byteCountToDisplaySize(999L));
        assertEquals("1.0 KB", FileUtils.byteCountToDisplaySize(1000L));
        assertEquals("1.0 KB", FileUtils.byteCountToDisplaySize(1023L));
        assertEquals("1.1 KB", FileUtils.byteCountToDisplaySize(1124L));
        assertEquals("1.1 KB", FileUtils.byteCountToDisplaySize(1164L));
        assertEquals("999 KB", FileUtils.byteCountToDisplaySize(1024L * 999L + 511L));
        assertEquals("1.0 MB", FileUtils.byteCountToDisplaySize(1024L * 999L + 512L));
        assertEquals("1.0 MB", FileUtils.byteCountToDisplaySize(1024L * 1024L - 1L));
        assertEquals("1.0 GB", FileUtils.byteCountToDisplaySize(1024L * 1024L * 1024L - 1L));
        assertEquals("1.0 TB", FileUtils.byteCountToDisplaySize(1024L * 1024L * 1024L * 1024L - 1L));
        assertEquals("1.0 PB", FileUtils.byteCountToDisplaySize(1024L * 1024L * 1024L * 1024L * 1024L - 1L));
        assertEquals("1.0 EB", FileUtils.byteCountToDisplaySize(
                1024L * 1024L * 1024L * 1024L * 1024L * 1024L - 1L));

        assertEquals("0 KB", FileUtils.byteCountToDisplaySize(100L, 2));
        assertEquals("1 KB", FileUtils.byteCountToDisplaySize(1000L, 2));
        assertEquals("1 KB", FileUtils.byteCountToDisplaySize(1023L, 2));
        assertEquals("20 KB", FileUtils.byteCountToDisplaySize(19L * 1024L + 512L, 2));
        assertEquals("20 KB", FileUtils.byteCountToDisplaySize(19L * 1024L + 512L, 3));
        assertEquals("19.5 KB", FileUtils.byteCountToDisplaySize(19L * 1024L + 512L, 4));
        assertEquals("196 KB", FileUtils.byteCountToDisplaySize(195L * 1024L + 512L, 4));

        assertEquals("0 MB", FileUtils.byteCountToDisplaySize(19L * 1024L + 512L, 1));
    }
}
{code})
    
> Rounding issue with byteCountToDisplaySize(long size)
> -----------------------------------------------------
>
>                 Key: IO-226
>                 URL: https://issues.apache.org/jira/browse/IO-226
>             Project: Commons IO
>          Issue Type: Improvement
>          Components: Utilities
>            Reporter: shu kai yuan
>             Fix For: 2.0
>
>         Attachments: roundedByteCountToDisplaySize.patch
>
>
> I do not understand the byteCountToDisplaySize(long size) method which is in  class FileUtils of the package org.apache.commons.io.
> If  the parameter size is 2047 , the method will return 1 KB.Why it will lose precision.
> I read the code. 
> Maybe it is a bug?

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira