/    Sign up×
Community /Pin to ProfileBookmark

Error in calculation?

I have some code that tell what currency (dollars, quarters, dimes, nickels, and pennies) to dispence based on an input number. Everything works fine except the calculation itself. It give the correct dollars, quarters, and dimes, but does not alway give the correct nickels and pennies. Here is the code I have:

<script type=”text/javascript”>
<!–
function evaluate (amt) {
amt.value = amt.value.match(/[d.]/g).join(”);
amt.form.dollars.value = Math.floor(amt.value / 1);
amt.form.quarters.value = Math.floor((amt.value % 1) / .25);
amt.form.dimes.value = Math.floor((amt.value % .25) / .10);
amt.form.nickles.value = Math.floor((amt.value % .10) / .05);
amt.form.pennies.value = Math.floor((amt.value % .05) / .01);
}
// –>
</script>
<form action=””>
<div>
<label>Amount<br><input name=”amount” type=”text” onchange=”evaluate(this)”></label>
<label>Dollars<br><input name=”dollars” type=”text”></label>
<label>Quarters<br><input name=”quarters” type=”text”></label>
<label>Dimes<br><input name=”dimes” type=”text”></label>
<label>Nickles<br><input name=”nickles” type=”text”></label>
<label>Pennies<br><input name=”pennies” type=”text”></label>
</form>

Thanks for your help in advance. I appreciate it.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MalgrimJul 05.2009 — The units you're using are not factors of each other, therefore your approach will not work.

The following code should do the trick: (Working with 4 digits is common practice, since you'll have errors resulting from the conversion to the binary system, that will transcend to the result if you simply use [I]floor[/I])

[CODE]var x = amt.value;
amt.form.dollars.value = Math.floor(Math.round(x * 10000) * .0001);
x = x - Math.floor(x);
amt.form.quarters.value = Math.floor(Math.round(x * 10000) * .0001 * 4);
x = x - Math.floor(x * 4) * .25;
amt.form.dimes.value = Math.floor(Math.round(x * 10000) * .0001 * 10);
x = x - Math.floor(x * 10) * .10;
amt.form.nickles.value = Math.floor(Math.round(x * 10000) * .0001 * 20);
x = x - Math.floor(x * 20) * .05;
amt.form.pennies.value = Math.floor(Math.round(x * 10000) * .0001 * 100); // might wanna use Math.round on the last one[/CODE]
Copy linkTweet thisAlerts:
@ed72ocauthorJul 05.2009 — I tried the code suggested and the nickels and pennies are still wrong. Say for instance in I enter .60 as the amount of change, it should tell me 2 quarters and 1 dime, instead it tells me 2 quarters, 1 dime, 1 nickel, and 4 pennies. If I enter .70 it tells me 2 quarters, 2 dimes, 3 nickel, and 3 pennies. What am i missing/doing wrong? Thanks again
Copy linkTweet thisAlerts:
@MalgrimJul 05.2009 — Sorry, my bad, I forgot to heed my own advice.

[CODE]var x = amt.value;
amt.form.dollars.value = Math.floor(Math.round(x * 10000) * .0001);
x = x - Math.floor(Math.round(x * 10000) * .0001);
amt.form.quarters.value = Math.floor(Math.round(x * 10000) * .0001 * 4);
x = x - Math.floor(Math.round(x * 10000) * .0001 * 4) * .25;
amt.form.dimes.value = Math.floor(Math.round(x * 10000) * .0001 * 10);
x = x - Math.floor(Math.round(x * 10000) * .0001 * 10) * .10;
amt.form.nickles.value = Math.floor(Math.round(x * 10000) * .0001 * 20);
x = x - Math.floor(Math.round(x * 10000) * .0001 * 20) * .05;
amt.form.pennies.value = Math.floor(Math.round(x * 10000) * .0001 * 100);[/CODE]
Copy linkTweet thisAlerts:
@ed72ocauthorJul 06.2009 — Thanks, that did it. I see what you are talking about now. Much appreciated.
×

Success!

Help @ed72oc 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.18,
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,
)...