cancel
Showing results for 
Search instead for 
Did you mean: 

Converting String to Double

Former Member
0 Kudos

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.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Sridhar,

I tried the code although it's changing the value according to my calculations still it doesen't change the value on the screen where I am trying for...

Looking forward to your reply.

sridhar_k2
Active Contributor
0 Kudos

I am not sure with your logic. You can print String before you set it to the Context.

like this,

Make sure your QuotasElement Cardinality 1.n.

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);

wdComponentAPI.getMessageManager().reportSuccess("str2 : "+str2 );

}

catch(Exception e)

{

e.printStackTrace();

}

wdContext.currentQuotasElement().setRest_Posted_Requested(str2);

Regards,

Sridhar

Former Member
0 Kudos

Hello Sridhar,

When I debug it I see the new value but some how it's not setting the Context Model attribute since it is a Model Attribute it does not have a cardinality property....

Thanks

sridhar_k2
Active Contributor
0 Kudos

I Dont see any wrong in your code. I would check the value before setting it to the context element. Check in the rest of your program, any where you are setting it to null.

Check one more time, is it setting correct values like this.

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);

wdComponentAPI.getMessageManager().reportSuccess("str2 : "+str2 );

}

catch(Exception e)

{

e.printStackTrace();

}

wdContext.currentQuotasElement().setRest_Posted_Requested(str2);

wdComponentAPI.getMessageManager().reportSuccess("str2 : "+str2 );

Are you getting any exception, or is it setting null to your context element?

Regards,

Sridhar

Former Member
0 Kudos

Hello Sridhar,

The statement

wdComponentAPI.getMessageManager().reportSuccess("str2 : "+str2 );

prints the right modified value on the top but it's just not getting reflected in the Table field....and I am not setting it to null.

Please help.

Message was edited by:

SubhashTripathi

sridhar_k2
Active Contributor
0 Kudos

Subhash,

I am not clear with your business logic. You want to convert String to Double, and you are converting that double value to String.

I have a Node - TestValueNode

- attribute1

- attribute2

String str = "123";

String str2 = null;

double d = 0.0;

try{

double d= Double.parseDouble(str); // this contains the double value

str2 = Double.toString(d); // this contains the String value same as 'str'

wdContext.currentTestValueNodeElement().setAttribute1(str2);

}catch(NumberFormatException nfe){

}

Write me back about your exact requirement. I can probably help you.

Regards,

Sridhar

Former Member
0 Kudos

No...No what I want to do is fetch a String from the Context ...convert it to a Double value ...perform some calculations on it ...convert it back to string again and set it to a context variable...

Looking forward to your reply.

Former Member
0 Kudos

Hi,

Assume you have a context value attribute called value of type String. You will read this String value>Convert it to double>Perform an increment operation-->Convert back to String.

String str = wdContext.currentContextElement().getValue();//eg."123"

double d = Double.parseDouble(str);// converted to 123.0

d = d+1;//124.0

wdContext.currentContextElement.setValue(Double.toString(d));//"124.0"

wdComponentAPI.getMessageManager().reportSuccess(wdContext.currentContextElement().getValue());

Warm Regards,

Murtuza

Warm Regards,

Murtuza

Former Member
0 Kudos

Hello Murtuza,

Thanks for the reply. I am also doing the same thing and when I try to print it ...it show me th eright value but when I set it to the Context variable ...it does not work.

Looking forward to your reply.

sridhar_k2
Active Contributor
0 Kudos

Subhash,

If you are getting correct value in both places print 1 and print 2, check your table cell, whether it is correctly mapped to the context element or not?

If table cell is binded to the context element, it should show the value. there no other mistakes, i couldn't see in your code.

String str = wdContext.currentContextElement().getValue();//eg.

wdComponentAPI.getMessageManager().reportSuccess(wdContext.currentContextElement().getValue()); - "123" - <b>Print 1</b>

double d = Double.parseDouble(str);// converted to 123.0

d = d+1;//124.0

wdContext.currentContextElement.setValue(Double.toString(d));//"124.0"

wdComponentAPI.getMessageManager().reportSuccess(wdContext.currentContextElement().getValue()); - <b>Print 2</b>

Regards,

Sridhar

Former Member
0 Kudos

Hi Subhash,

wdComponentAPI.getMessageManager().reportSuccess(wdContext.currentContextElement().getValue());

The above line prints the value in the context itself and for me it printed the correct output.

Regards,

Murtuza

sridhar_k2
Active Contributor
0 Kudos

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.

}

Former Member
0 Kudos

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.

sridhar_k2
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hello Sridhar,

I am almost trying the same thing but if i put my last statement outside the Try it shows a compilation error and if I put inside it ...it does not do anything if I debug it ...at runtime it says "The String cannot be resolved"..

Thanks

Looking forward to your reply.

sridhar_k2
Active Contributor
0 Kudos

Plz send me the code, after keeping it out side try / catch. Send me the error also.

Regards,

Sridhar

Former Member
0 Kudos

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

Former Member
0 Kudos

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

sridhar_k2
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi! Subhash,

it's Double not Doubel.

following code is working fine..

double d = Double.parseDouble("100");

regards,

Mithileshwar