I am currently doing a coursework which involves purchasing tram tickets and adding or removing them from a grand total. I have been able to achieve this but have struggled to get another decimal point on all my values. for example If a user wants to buy one adult single ticket at the price of £2.00 it should come up in the sub-total as £2.00, but on my work it comes up with 2. I have copyed my html code below,I do apologise about the size of the code but it was easier to show everything i have done. So you can understand it better.
<script type=”text/JavaScript”>
<!–
function add_ticket($fare) {
if ($fare == ‘adult_single’) {
$cost = 2.00;
}
if ($fare == ‘adult_return’) {
$cost = 2.4;
}
if ($fare == ‘adult_allday’) {
$cost = 2.8;
}
if ($fare == ‘child_single’) {
$cost = 1.2;
}
if ($fare == ‘child_return’) {
$cost = 1.8;
}
if ($fare == ‘child_allday’) {
$cost = 2.2;
}
if ($fare == ‘student_single’) {
$cost = 1.20;
}
if ($fare == ‘student_return’) {
$cost = 1.60;
}
if ($fare == ‘student_allday’) {
$cost = 2;
}
if ($fare == ‘senior_single’) {
$cost = 1.20;
}
if ($fare == ‘senior_return’) {
$cost = 1.6;
}
if ($fare == ‘senior_allday’) {
$cost = 2;
}
document.getElementById($fare+’_qty’).value = parseInt(document.getElementById($fare+’_qty’).value) + 1;
document.getElementById($fare+’_cost’).value = parseFloat(document.getElementById($fare+’_cost’).value) + $cost;
update_total();
}
function remove_ticket($fare) {
if ($fare == ‘adult_single’) {
$cost = 2.00;
}
if ($fare == ‘adult_return’) {
$cost = 2.4;
}
if ($fare == ‘adult_allday’) {
$cost = 2.8;
}
if ($fare == ‘child_single’) {
$cost = 1.2;
}
if ($fare == ‘child_return’) {
$cost = 1.8;
}
if ($fare == ‘child_allday’) {
$cost = 2.20;
}
if ($fare == ‘student_single’) {
$cost = 1.20;
}
if ($fare == ‘student_return’) {
$cost = 1.60;
}
if ($fare == ‘student_allday’) {
$cost = 2;
}
if ($fare == ‘senior_single’) {
$cost = 1.2;
}
if ($fare == ‘senior_return’) {
$cost = 1.8
;
}
if ($fare == ‘senior_allday’) {
$cost = 2;
}
if (document.getElementById($fare+’_qty’).value > 0 ) {
document.getElementById($fare+’_qty’).value = parseInt(document.getElementById($fare+’_qty’).value) – 1;
document.getElementById($fare+’_cost’).value = parseFloat(document.getElementById($fare+’_cost’).value) – $cost;
update_total();
}
}
function update_total() {
$total = 0;
$total = parseFloat(document.getElementById(‘adult_single_cost’).value) + parseFloat(document.getElementById(‘adult_return_cost’).value) + parseFloat(document.getElementById(‘adult_allday_cost’).value) + parseFloat(document.getElementById(‘senior_single_cost’).value) + parseFloat(document.getElementById(‘senior_return_cost’).value) + parseFloat(document.getElementById(‘senior_allday_cost’).value) + parseFloat(document.getElementById(‘student_single_cost’).value) + parseFloat(document.getElementById(‘student_return_cost’).value) + parseFloat(document.getElementById(‘student_allday_cost’).value) + parseFloat(document.getElementById(‘child_single_cost’).value) + parseFloat(document.getElementById(‘child_return_cost’).value) + parseFloat(document.getElementById(‘child_allday_cost’).value);
document.getElementById(‘total’).value = $total;
}
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+”.location='”+args[i+1]+”‘”);
}
//–>
</script>
IN <body>
<input name=”-” type=”button” id=”-” style=”width: 150px;height: 100px; background: #FFFF99; color: #000000; onclick=” onclick=”remove_ticket(‘adult_single’)”‘)” value=”-“remove_ticket(‘adult_single />
If anyone knows how to added an extra decimal place, please help. I will appriciate any feedback which will assist me.
Thank you,
Raj