View Full Version : Converting cents to dollars
Muffin_LA
19-10-2005, 16:07/04:07PM
Hi everyone:
I figured out the last script and got perfect on it. Go figure.
What I am trying to figure out is how to convert cents to dollars. The user enters an amount in cents and the script converts it to dollars.
Any help would be appreciated.
Thanks
Linda
chrishirst
19-10-2005, 16:44/04:44PM
<script type="text/javascript">
var divisor = 100
cents = 199
document.write(parseInt(cents / divisor) + " Dollars and ")
document.write((cents % divisor) + " Cents")
</script>
I'll let you work out what does what :D
Muffin_LA
20-10-2005, 15:34/03:34PM
Thanks for the help .. but I am still lost.
What I need is to have a text box where the user enters an amount in cents.. and then clicks the button and an alert appears and shows the amount in cents and dollars..
More hints.. pleaseeeeeeeeee:confused:
Thanks
Linda
Quadrille
20-10-2005, 15:49/03:49PM
So, a code that divides by 100, and changes the label from "$" to "cents"?
Muffin_LA
20-10-2005, 15:58/03:58PM
A code that will show the user the cents they entered.. converted into dollars and cents... for example.. if they enter 150 cents.. the alert will show $1 and 50 cents.
Quadrille
20-10-2005, 16:03/04:03PM
Originally posted by Quadrille
So, a code that divides by 100, and changes the label from "$" to "cents"? Sorry, I meant a code that divides by 100, and changes the label from "cents" to "$"?
Muffin_LA
20-10-2005, 16:04/04:04PM
Yes please
Any help would be greatly appreciated
Muffin_LA
20-10-2005, 18:02/06:02PM
This is my code so far. When the alert box comes up it shows the dollar and cents.. and then the cents again.. for example.. if I typed in 44667... it would show.
The conversion is $446.67 and 67 cents.
how can I get it to show $446 and 67 cents?
function centConvert() {
var cents = document.centDollar.cents.value
dollarResult = cents / 100;
centsResult = cents % 100;
window.alert("The conversion is $ " + dollarResult + " and " + centsResult + " and cents.");
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<form name="centDollar" action="">
<p>
<input type="text" name="cents" />
<input type="button" name="total" value="Conversion" onclick="centConvert()" /></p>
</form>
</body>
</html>
chrishirst
20-10-2005, 18:26/06:26PM
The bit you are mising is in my code, parseInt(),
parseInt(cents / 100) will effectively give you an integer division
g1smd
20-10-2005, 18:28/06:28PM
You need a function that takes off all after the decimal point.
Inspect the original code (way up at top of thread) for a way that might work.
Muffin_LA
21-10-2005, 02:10/02:10AM
Thank you for all your help.
I just dont know where to add the parseInt in the code. I have been putting it here and there and its not working for me.
Muffin_LA
21-10-2005, 02:20/02:20AM
I got it to work.. thanks so much everyone.
Greatly appreciated :)
Linda
g1smd
21-10-2005, 07:37/07:37AM
Another kids homework done "on the net".
Wonder if teacher reads in here? :D :D :D :D :D :D :D :D :D :D
Muffin_LA
21-10-2005, 14:50/02:50PM
Kid??.. lol.. I wish.. I am almost 40.
My teacher said any help is great. As long as we learn from it and understand it. Thats why I dont submit someone elses work. I get help and learn from it and understand it and then send in my own work
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.