commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Juan Pablo Santos Rodríguez (JIRA) <j...@apache.org>
Subject [jira] [Commented] (LANG-1016) NumberUtils#isParseable method(s)
Date Wed, 04 Jun 2014 08:57:01 GMT

    [ https://issues.apache.org/jira/browse/LANG-1016?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14017512#comment-14017512
] 

Juan Pablo Santos Rodríguez commented on LANG-1016:
---------------------------------------------------

Giving it a second thought, a more suitable name for the method could be {{isHumanReadableNumber}}.

Test would look like:
{code}
    @Test
    public void testIsHumanReadableNumber() {
        assertFalse( NumberUtils.isHumanReadableNumber( null ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "" ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "0xC1AB" ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "65CBA2" ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "pendro" ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "64,2" ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "64.2.2" ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "64." ) );
        assertFalse( NumberUtils.isHumanReadableNumber( "64L" ) );
        assertTrue( NumberUtils.isHumanReadableNumber( "64.2" ) );
        assertTrue( NumberUtils.isHumanReadableNumber( "64" ) );
        assertTrue( NumberUtils.isHumanReadableNumber( "018" ) );
    }
{code}

whereas one possible approach to method could be:
{code}
    /**
     * <p>Checks whether the String a human readable number.</p>
     *
     * <p>Human readable numbers include those Strings understood by <code>Integer.parseInt(String)</code>,
     * <code>Long.parseLong(String)</code>, <code>Float.parseFloat(String)</code>
or
     * <code>Double.parseDouble(String)</code>.</p>
     *
     * <p>Hexadecimal and scientific notations are <strong>not</strong>
considered human-readable.
     * See {#isNumber(String)} on those cases.</p>
     *
     * <p><code>Null</code> and empty String will return <code>false</code>.</p>
     *
     * @param str the <code>String</code> to check
     * @return <code>true</code> if the string is a human readable number
     */
    public static boolean isHumanReadableNumber( final String str ) {
        if( StringUtils.endsWith( str, "." ) ) {
            return false;
        }
        return isDigits( StringUtils.replaceOnce( str, ".", StringUtils.EMPTY ) );
    }
{code}

(apologies on not including a proper patch, but code is so small, it fits on one comment in
here..)

> NumberUtils#isParseable method(s)
> ---------------------------------
>
>                 Key: LANG-1016
>                 URL: https://issues.apache.org/jira/browse/LANG-1016
>             Project: Commons Lang
>          Issue Type: Wish
>          Components: lang.math.*
>            Reporter: Juan Pablo Santos Rodríguez
>             Fix For: Patch Needed
>
>
> (for background see [LANG-997|https://issues.apache.org/jira/browse/LANG-997?focusedCommentId=13991193&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13991193])
> It would be nice to have an {{isParseable}} method (or whatever it may be called), to
be able to identify valid (=parseable) Numbers, something along the lines {{toDouble(String)}}
/ {{toLong(String)}} etc., but returning {{true}} / {{false}} while avoiding at the same time
the {{try/catch(NumberFormatException)}} of those methods.
> This method would be similar to {{isNumber}}, but it should yield {{true}} for invalid
octals like "018" which are parseable as Numbers. The point of this method is to identify
"human" (neither hex nor octal, but should handle decimal points) numbers stored as Strings,

> We are using NumberUtils#isNumber to identify valid (parseable, human) numbers, but as
of 3.3, this method also handles octal numbers, so, f.ex.,  018, which was recognized as a
valid number, isn't recognized anymore as one.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message