on ‎2007 Oct 18 4:06 PM
Hello,
I am trying to convert a string to double as:
double d= Doubel.parseDouble(String)
but at runtime it is giving NumberFormatException ....
how do we convert String to Double else....
Any help would be highly appreciated.
Request clarification before answering.
Hello Subhash,
If your String is valid number as String, you can convert it to Double. If it contains other than numbers, you cannot conver it to Double.
ex: -
double d= Double.parseDouble("123");
you can use like this,
try{
double d= Double.parseDouble("123a");
}catch(NumberFormatException exp){
//throw erro.
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hello Sridhar,
Thanks for the reply. That was the problem. But after getting through it and processing it I am trying to set the String to the Context Element which is tied to a UI Table field ...but does not make any difference ...when I try to debug it says that the String cannot be resolved..
Looking forward to your reply.
Subhash,
If i understood your requirement correctly, you want to convert String to double and want to set the String to your UI Element. If that is true,
use try, catch to convert string to double and after catch, you set your contest element.
like this
try{
double d= Double.parseDouble("12a3");
}catch(NumberFormatException nfe){
}
wdComponentAPI.getMessageManager().reportSuccess("Exp Occured before");
wdContext.currentContest.setStringMsg("12a3");
Hope, it is working for you.
Regards,
Sridhar
Presumptions:
Context:
-quantity: String
-node: items
*id
*qty
*price
*total
View:
-Table.id: table - dataSource: items
SRC:
java.text.DecimalFormat df = new java.text.DecimalFormat("#,##0.00");
wdContext.nodeItems().invalidate();
try {
// is quantity valid?
double qty = Double.parseDouble(wdContext.currentContextElement().getQuantity());
// valid quantity
for(int i=0; i<10; i++) {
IPublic<viewcontroller>.IItemsElement item = wdContext.nodeItems().createItemsElement();
item.setId(String.valueOf(i+1));
item.setQty(wdContext.currentContextElement().getQuantity());
item.setPrice(df.format((i+1) * 1.0)); // *1.0... cast int to double
item.setTotal(df.format(qty * (i+1)));
wdContext.nodeItems().addElement(item);
}
} catch(NumberFormatException nfe) {
wdComponentAPI.getMessageManager().reportException(nfe, true);
}good luck...
Message was edited by:
Carlos Rojas Yasuda
Sridhar,
Here you go:
try
{
for(int i=0;i<str.length();i++)
{
char c =str.charAt(i);
if(c!=' '&& c<65)
final_str=final_str+c;
}
double balance =Double.parseDouble(final_str);
balance = balance + addition;
String str2 = Double.toString(balance);
}
catch(Exception e)
{
e.printStackTrace();
}
wdContext.currentQuotasElement).setRest_Posted_Requested(str2);
It points to the last line saying str2 cannot be resolved ...
Looking forward to your reply.
}
Message was edited by:
SubhashTripathi
Subhash,
The below code will work for you. String str2 was in side loop (local to the loop), it wont visible to the out of the loop.
String str2 = null;
try
{
for(int i=0;i<str.length();i++)
{
char c =str.charAt(i);
if(c!=' '&& c><65)
final_str=final_str+c;
}
double balance =Double.parseDouble(final_str);
balance = balance + addition;
str2 = Double.toString(balance);
}
catch(Exception e)
{
e.printStackTrace();
}
wdContext.currentQuotasElement).setRest_Posted_Requested(str2);
Let me know, if it doesnt work.
Regards,
Sridhar
| User | Count |
|---|---|
| 15 | |
| 9 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.