/    Sign up×
Community /Pin to ProfileBookmark

Decimal place HELP!!!!

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

to post a comment
HTML

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 08.2008 — [code=php]document.getElementById('total').value = $total.toFixed(2);[/code]should be the last line of your update_total function, but could you please use the forums [noparse][code=php] and [/code] OR and [/noparse] tags to reduce the size of the post?

edit: minor errors
Copy linkTweet thisAlerts:
@jeeves_2008authorJan 08.2008 — thank you very much, that works with the grand total but would i be able to do the same thing with the sub-totals?. Sorry about the long piece of code again, it is my first time posting on a forum, i will use them in the future.

Thank you again,

Raj
Copy linkTweet thisAlerts:
@scragarJan 08.2008 — just add .toFixed(2) to the end of anything getting show to the user.
Copy linkTweet thisAlerts:
@jeeves_2008authorJan 08.2008 — Sorry for being so thick, but were i keep on adding .toFixed(2), none of the buttons work. When i take that out it works again for the grand total. Do you know were i should add .toFixed(2) for the sub-total.

Thank you so much for helping me, i am in such a pickle.

Raj
Copy linkTweet thisAlerts:
@scragarJan 08.2008 — [code=php]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.2;
if ($fare == 'student_return')
$cost = 1.6;
if ($fare == 'student_allday')
$cost = 2;
if ($fare == 'senior_single')
$cost = 1.2;
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).toFixed(2);
document.getElementById($fare+'_cost').value = (parseFloat(document.getElementById($fare+'_cost').value) + $cost).toFixed(2);
update_total();
};
function remove_ticket($fare) {
if ($fare == 'adult_single')
$cost = 2;
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).toFixed(2);
document.getElementById($fare+'_cost').value = (parseFloat(document.getElementById($fare+'_cost').value) - $cost).toFixed(2);
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;
};
[/code]
that should be about it, let me know if I missed anything. and you could do with making your balue checking run through an array, reduce the number of lines a bit.
Copy linkTweet thisAlerts:
@jeeves_2008authorJan 08.2008 — Thats awesome, cheers. That works perfectly for what i need it for. You dont know how much you have helped me.

Thanks again, Respect

Raj
Copy linkTweet thisAlerts:
@jeeves_2008authorJan 08.2008 — Hello , hopefully you can assit me again. I am trying to output the total number of tickets and the grand total on another html page. I have tryd the below code but no go. Could you please help me again.


<script type="text/JavaScript">

function MM_goToURL() {

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]+"'");

}



[HTML ]



<h3>Number of tickets:<? echo $_POST['total']; ?></h3>
<h3>Cost:<? echo$_GET[$total]; ?></h3>



I know that you will have other people to help, but i would be very greatful if you could solve this for me.

Thanks

Raj
×

Success!

Help @jeeves_2008 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.6,
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,
)...