Author Topic: Add Commas to Long Numbers  (Read 11998 times)

0 Members and 1 Guest are viewing this topic.

Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Add Commas to Long Numbers
« on: May 01, 2013, 06:05:54 AM »
Recently I had a project where the database contained several numbers that were very long; into the billions. I had to enter them into the database as nondelimited numbers (no commas) because I was using quite a bit of javascript math functions. So I want to be able to display the numbers in some areas of the templates with commas. I found this nice work around on Stack Overflow:

http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript

Code: [Select]
<script>
function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
document.write(numberWithCommas([[field_with_large_number]]));
</script>

I added the document.write statement for those of you that do not know how to call a function like this. Hope this helps others.

Jason
Founder of www.slipdex.com

Offline nadim123

  • Newbie
  • *
  • Posts: 1
Re: Add Commas to Long Numbers
« Reply #1 on: July 02, 2013, 10:03:28 AM »
Thanks for this code, I am searching from long time.I need this code in generating bills. I will run this code first but before that thank you once again or this code.