Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5E984FC2A for ; Sun, 24 Mar 2013 22:49:16 +0000 (UTC) Received: (qmail 88977 invoked by uid 500); 24 Mar 2013 22:49:15 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 88891 invoked by uid 500); 24 Mar 2013 22:49:15 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 88573 invoked by uid 99); 24 Mar 2013 22:49:15 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 24 Mar 2013 22:49:15 +0000 Date: Sun, 24 Mar 2013 22:49:15 +0000 (UTC) From: "Mark (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (IO-373) FileUtils.byteCountToDisplaySize improvement/rounding issues MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/IO-373?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13612282#comment-13612282 ] Mark commented on IO-373: ------------------------- 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} > FileUtils.byteCountToDisplaySize improvement/rounding issues > ------------------------------------------------------------ > > Key: IO-373 > URL: https://issues.apache.org/jira/browse/IO-373 > Project: Commons IO > Issue Type: Improvement > Components: Utilities > Affects Versions: 2.4 > Reporter: Mark > Priority: Minor > > Issue IO-226 is not fixed but closed. > ? > Here is my solution that also support a user-defined precision in terms of a maximum length of the digits part. -- 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