Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with conversion from string in calculation

Status
Not open for further replies.

austingal

Programmer
Feb 23, 2006
4
US
I have been handed over an application that I did not create :) and am fairly new to javascript. My problem is a simple calculation formVal1 = ((1000000 - formVal3) + formVal2) * .0096 I discovered that + is concatenated when adding strings so I need to convert formVal2 to a number. I've tried several things but nothing works.

Thanks!



</script>

<script language = JavaScript>

function CalculateMWh(formItem1,formItem2,formItem3,formItem4) {

var formVal1 = document.getElementById(formItem1).value;
var formVal2 = document.getElementById(formItem2).value;
var formVal3 = document.getElementById(formItem3).value;
var formVal4 = document.getElementById(formItem4).value;
// if (document.getElementById(formItem1).type != 'hidden')
if (formItem1 == 'P8_STDBY_2_AMT' || formItem1 == 'P8_STDBY_MWH')
{
if (document.getElementById(formItem4).value != 107)
{
formVal1 = ((1000000 - formVal3) + formVal2) * .0096

}
else
{
formVal1 = formVal2-formVal3;
}
}
else
{
formVal1 = formVal2-formVal3;
}
document.getElementById(formItem1).value = formVal1;
if (formVal4 == 'P8_AUX_2_AMT')

{
document.getElementById(formItem4).value = formVal1
}
}</script>
 
Just do a subtraction of 0.
If formVal2 is being treated as a string doing this:
formVal2-0 will turn it into a number providing it is a numerical value in that field to begin with.

formVal1 = ((1000000 - (formVal3-0)) + (formVal2-0)) * .0096

Stamp out, eliminate and abolish redundancy!
 
Actually, any mathematical function applied to a string value will cause it to type-cast except for +, as in:
a = b + c;

However, you can use the + as a unary operator like this:
a = b + (+c);
which will cause b to type-cast
You could also write it as:
a = b + +c;
The parens around the second example help to make it clear what the operation is doing as well as ensuring the order of operation.

Using unary + is the fastest method but I have always just done a subtraction of zero as it is clearer code for others to read.

You could just as easily do any other mathematical operation that does not affect the value returned like:
a = b - 0;
a = b / 1;
a = b * 1;
All of the examples convert the type without altering the value.


Stamp out, eliminate and abolish redundancy!
 
While I generally use the format
Code:
[b]varname * 1[/b]
for this kind of thing, like one of theniteowl's examples, there's a specific function in Javascript to make sure the variable is treated like an integral number:
Code:
[b]parseInt(varname, basenumber);[/b]

formVal1 = ((1000000 - formVal3) + parseInt(formVal2, 10)) * .0096;

There's also a function for floating point numbers that does the same thing:

Code:
parseFloat(varname);

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top