/    Sign up×
Community /Pin to ProfileBookmark

decimal place help please

Hi,

I have downloaded a javascript that is just about perfect for my project. The only thing it doesn’t do….I have tried hard to make it do this….is to give the result with two decimal places. For example, if I add (say) 21.20 and 21.20, the answer is 42.4 but I really want it to say 42.40. I have been experimenting with ‘toFixed(2)’ but I can’t get this to work. Can anyone help me please? Many thanks.

Here is the code….

<script type=”text/javascript”>

function startCalc(){
interval = setInterval(“calc()”,1);
}
function calc(){
one = document.autoSumForm.firstBox.value;
two = document.autoSumForm.secondBox.value;
document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
clearInterval(interval);
}
// End –>
</script>
</HEAD>

<BODY>

<form name=”autoSumForm”>
<input type=text name=”firstBox” value=”” onFocus=”startCalc();” onBlur=”stopCalc();”> +
<input type=text name=”secondBox” value=”” onFocus=”startCalc();” onBlur=”stopCalc();”> =
<input type=text name=”thirdBox”>
</form>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — ...give the result with two decimal places.[/QUOTE]
Change this:

document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);

to this:

document.autoSumForm.thirdBox.value = ((one * 1) + (two * 1)).toFixed(2);
Copy linkTweet thisAlerts:
@realjumperauthorJun 07.2005 — Thanks for the reply. Unfortunately this doesn't seem to work....it stops the script altogether!!
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — If it stops the script altogether, then there is an error message being generated. I need to know what is that error message.
Copy linkTweet thisAlerts:
@realjumperauthorJun 07.2005 — Ther error says:

Microsoft JScript runtime error

Line 18

Char 1

Error Object doesn't support this property or method

.....and line 18 is:

document.autoSumForm.thirdBox.value = ((one * 1) + (two * 1)).toFixed(2);

Thanks for your help
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — On what platform, browser, and version are you testing your page?
Copy linkTweet thisAlerts:
@realjumperauthorJun 07.2005 — This will be used on an Intranet, which is running MAC (OS 9 and OS X) computers. The favoured browser is IE 5.2, which is the one this script won't for. I have tested on Opera and Safari, both of which execute the script perfectly. So it's just got a problem with IE 5.2 on the Mac
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — Yep, 'twould appear that the [b]toFixed()[/b] method is not implemented on that browser for that platform. Something like the following will create that method if it is missing:
<i>
</i>if (!Number.prototype.toFixed) {
Number.prototype.toFixed = function(dec) {
if (isNaN(this)) return NaN;
if (isNaN(dec)) dec = 0;
if (dec &lt; 0) dec = 0;
if (dec &gt; 9) dec = 9;
var val = Math.round(this * Math.pow(10,dec));
val = (val / Math.pow(10,dec)).toString(10);
if (dec &gt; 0) {
var p = val.indexOf(".");
if (p &lt; 0) val += ".";
var p = val.indexOf(".") + 1;
val = (val+"000000000").substr(0,p+dec);
}
return val;
}
}
Copy linkTweet thisAlerts:
@realjumperauthorJun 07.2005 — WOW!!! That is very impressive.....and it works like a charm. Thank you so much for your tremendous help. Now I'm going to see if I can figure out how this whole script works.

Many, many thanks
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — It's not tested very well, by the way. I'm also sure it could be done a bit more efficiently. But... ?
×

Success!

Help @realjumper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...