NumberFormat Parse Exception

I have a requirement where user can enter positive values like +1 or Negative Values like -1, now the problem is i need to parse it but it is giving an error, i need any way to parse.

Below is the Sample Code :

        DecimalFormat df = new DecimalFormat();
        Number n = df.parse("+111");
        double d = n.doubleValue();
        System.out.println(" val - " +d);

Iam Getting an error ..Unparseable number: "+111"

I know if we set      df.setPositivePrefix("+"); and then parse it, it works, but its not working if we dont give + its giving again error..

I have a way that is
Number num = decFormat.parse(sValue, new ParsePosition(((sValue.indexOf('+') == -1)?0:1)));

we are just skipping '+', but i dont wanna need this...

does anybody have idea about this...please...

 
ekarthaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
How about:

df = new DecimalFormat () ;

if ( sValue.startsWith ( '+' ) )
  df.setPositivePrefix ( "+" ) ; // end if

Number n = df.parse ( sValue ) ;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tommy BraasCommented:
May I suggest:

double value = Double.parseDouble("+111").doubleValue();
rama_krishna580Commented:
Hi,

Example See here:

public static String getDecimal(Number value, Locale locale, String pattern)
{
        NumberFormat formatter;

        if (locale==null)
            formatter = new java.text.DecimalFormat(pattern);
        else
        {
            formatter = NumberFormat.getNumberInstance(locale);
            DecimalFormat df = (DecimalFormat) formatter;
            df.applyPattern(pattern);
            return df.format(value);
        }
        return (formatter.format(value));
    }

R.K
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

rama_krishna580Commented:
Hi,

look at here also...

    public void Test4070798 ()
   {
        NumberFormat formatter;
        String tempString;
        formatter = NumberFormat.getNumberInstance(Locale.FRANCE);
        tempString = formatter.format (-5789.9876);

        if (tempString.equals(expectedDefault)) {
            logln ("default test passed.");
        } else {
            errln("Failed:" +
            " Expected " + expectedDefault +
            " Received " + tempString );
        }


        formatter = NumberFormat.getCurrencyInstance(Locale.FRANCE);
        tempString = formatter.format( 5789.9876 );

        if (tempString.equals(expectedCurrency) ) {
            logln ("currency test assed.");
        } else {
            errln("Failed:" +
            " Expected " + expectedCurrency +
            " Received " + tempString );
        }


        formatter = NumberFormat.getPercentInstance(Locale.FRANCE);
        tempString = formatter.format (-5789.9876);

        if (tempString.equals(expectedPercent) ) {
            logln ("percentage test passed.");
        } else {
            errln("Failed:" +
            " Expected " + expectedPercent +
            " Received " + tempString );
        }
    }

R.K
ekarthaAuthor Commented:
Hey i dont have information of Locale, its in general..and i dont have information about particular decimal format also..

i have a way we need to write our class which extends decimalformat class ,in that class we can do what ever manipulation we need to do..i mean decimalformat is a low level class so we need to implement a high level class which serves our purpose.

Any how thanks for everybody for your contribution.
Mayank SAssociate Director - Product EngineeringCommented:
What are the possible formats?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.