math with int and floats doesn't work?

E

Eric Gibson

I could have sworn this would be a FAQ, but it doesn't appear to be in
there. You must get this all the time, or I'm just totally doing something
wrong.

Math with int's and floats appears to just not work at all, much to my
frustration. :-( I'm trying to write a simple calculation and it just plain
outputs wrong information. Something like this:

function calculateValue(kWh) {
dollar_amount = kWh * .033;
// dollar_amount = StrS(dollar_amount, 1, 2);
result.innerHTML = dollar_amount;
}

I even jumped through hoops to try to enforce type, and convert strings to
float format, but it still doesn't work (which is wierd in itself).

function calculateValue(inputValue) {
var kWh = new String(inputValue);
kWh = parseFloat(kWh);
dollar_amount = kWh * .033;
dollar_amount = StrS(dollar_amount, 1, 2);
result.innerHTML = dollar_amount;
}

I've seen float errors in calculators, and other languages, but nothing that
was just totally wrong everytime by a power of ~10, it's a little insane!

Eric
 
L

Lasse Reichstein Nielsen

Eric Gibson said:
I could have sworn this would be a FAQ, but it doesn't appear to be in
there. You must get this all the time, or I'm just totally doing something
wrong.

If your results are so surpricing, my bet is that you are doing
something wrong. Javascript isn't significantly different from other
languages in how it handles numbers.
Math with int's and floats appears to just not work at all, much to my
frustration. :-(

I'm *pretty* sure you are doing something wrong then :).
I'm trying to write a simple calculation and it just plain outputs
wrong information. Something like this:

function calculateValue(kWh) {
dollar_amount = kWh * .033;
// dollar_amount = StrS(dollar_amount, 1, 2);
result.innerHTML = dollar_amount;
}

The three questions that needs answers before anyone can help you:
What do you do? What is it supposed to do? What does it do?
The first is almost answered by the code, except: Where is it called
and with what as an argument?
I even jumped through hoops to try to enforce type, and convert strings to
float format, but it still doesn't work (which is wierd in itself).

The multiplication should convert its other arguments to numbers, similar
to using the Number conversion function.
function calculateValue(inputValue) {
var kWh = new String(inputValue);
kWh = parseFloat(kWh);
dollar_amount = kWh * .033;
dollar_amount = StrS(dollar_amount, 1, 2);
result.innerHTML = dollar_amount;
}

I've seen float errors in calculators, and other languages, but nothing that
was just totally wrong everytime by a power of ~10, it's a little insane!

I don't know the function StrS, but the rest looks fine.

If it is off by a factor of 10, are you sure the input is correct, and
that .033 is the correct multiplyer (and it shouldn't have been 0.33)?

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
I could have sworn this would be a FAQ, but it doesn't appear to be in
there. You must get this all the time, or I'm just totally doing something
wrong.

Math with int's and floats appears to just not work at all, much to my
frustration. :-( I'm trying to write a simple calculation and it just plain
outputs wrong information. Something like this:

function calculateValue(kWh) {
dollar_amount = kWh * .033;
// dollar_amount = StrS(dollar_amount, 1, 2);
result.innerHTML = dollar_amount;
}

The arithmetic works for me. I trust that you actually supplied the
function StrS, and the functions Sign and StrU which it calls.


One should never write a number with the first or last character a
decimal point, since that is liable to human error. But, assuming that
you meant 0.033, that's not your problem; but your electricity is too
cheap.

For something like this, you should always give a complete "non-working"
answer, with data, together with what you wish the function to do and
the results that you expect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,101
Messages
2,570,637
Members
47,243
Latest member
RustyPalin

Latest Threads

Top