4

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>
hlumley
  • 65
  • 1
  • 6

1 Answers1

1

This appears to work for your simple use-case

var answer = salary.toFixed(2);                     // convert to string
answer = answer.replace(/(\d)(\d{3})\./, "$1,$2."); // look for four digits and '.'

shorter regexps may be available...

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • I guess you rather need a longer regex :-) Also, yours does only work for 4-digit numbers – Bergi Jun 05 '13 at 14:58
  • why need a longer one? It does also work for 5 and 6 digits, BTW. – Alnitak Jun 05 '13 at 14:59
  • Just meant that the ["standard" pattern](http://stackoverflow.com/a/2901298/1048572) which handles more general cases is longer - you hardly will find something shorter than you have. – Bergi Jun 05 '13 at 15:07
  • Thank you very much! That worked. I apologize of this was a duplicate post. The second part of the issue I had was unique, but your answer for the first part actually made everything perfect the way I need it. Thanks again! – hlumley Jun 05 '13 at 20:40
  • if by the second part you mean the trailing `0` to get two digits after the decimal point, that's what the first line does. – Alnitak Jun 05 '13 at 20:43
  • Exactly. Yes, that's correct. I realize my error now and what the code you provided is doing. It really helped and I really appreciate it. It's funny b/c when I had started writing this code, I looked at doing a toFixed and for whatever reason I didn't. I know better next time. :P – hlumley Jun 05 '13 at 20:50
  • Yes, def. I tried to vote and it says Vote up requires 15 reputation. I'm assuming since this is my first post, I don't have the rep yet. I did however click the checkmark and it is now green as the accepted answer. :) – hlumley Jun 05 '13 at 21:09