By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

math with int and floats doesn't work?

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Eric Gibson" <ai*****@bellsouth.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
JRS: In article <tz***************@bignews5.bellsouth.net>, seen in
news:comp.lang.javascript, Eric Gibson <ai*****@bellsouth.net> posted at
Sat, 27 Sep 2003 17:49:16 :-
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

P: n/a
JRS: In article <he**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sun, 28 Sep 2003 00:27:06 :-

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


Even you should read the FAQ !

StrS is also in <URL:http://www.merlyn.demon.co.uk/js-round.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.