[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# NumberFormat Parse Exception

Posted on 2006-04-18
Medium Priority
356 Views
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...

0
Question by:ekartha

LVL 30

Accepted Solution

Mayank S earned 136 total points
ID: 16478864

df = new DecimalFormat () ;

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

Number n = df.parse ( sValue ) ;
0

LVL 14

Assisted Solution

Tommy Braas earned 132 total points
ID: 16479256
May I suggest:

double value = Double.parseDouble("+111").doubleValue();
0

LVL 23

Assisted Solution

rama_krishna580 earned 132 total points
ID: 16479300
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

LVL 23

Expert Comment

ID: 16479337
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

Author Comment

ID: 16485738
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

LVL 30

Expert Comment

ID: 16485768
What are the possible formats?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
###### Suggested Courses
Course of the Month20 days, left to enroll

#### 872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.