I am writing a javascript code that allows a user to calculate what their bonus will be. It's simple math. Annual Salary x Payout Percentage = Your Payout.
Everything is built and works, but I have a format question.
Instead of it presenting your payout as $4000.3, I'd like a comma in there and an added zero in the cents portion so it reads... $4,000.30. (this amount is just an example)
I'm not sure how to do this. In my code calculation below, I do the math of salary * payout / 100, and then a Math.round so that it will round it two decimal places, but I can't figure out what to add or tweak to make it add a comma in the thousands (it will never be in the millions by the way) and then add a zero at the end if like in the example above, it's leaving it out for .30.
Any help is appreciated. I just need to be guided in the right direction. Thanks!
Here's the code for this portion of the calculator:
<script type="text/javascript">
function calculate()
{
var salary = document.calculateBonus.salary.value;
var payout = document.calculateBonus.payout.value;
var answer = (salary * payout / 100);
answer = Math.round(answer * 100) / 100;
if(document.calculateBonus.salary.value == "" )
{
alert("Please enter your salary");
document.calculateBonus.salary.focus();
return false;
}
if(document.calculateBonus.payout.value == "" )
{
alert("Please put in your payout percentage");
document.calculateBonus.payout.focus();
return false;
}
(document.getElementById('payoutText').innerHTML =
("Your Bonus Payout is $" + answer));
return false;
}
</script>