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?
 
Mayank SAssociate Director - Product EngineeringCommented:
How about:

df = new DecimalFormat () ;

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

Number n = df.parse ( sValue ) ;
0
 
Tommy BraasCommented:
May I suggest:

double value = Double.parseDouble("+111").doubleValue();
0
 
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
0
 
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
What are the possible formats?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.