{"id":2634,"date":"2012-01-05T21:40:04","date_gmt":"2012-01-06T02:40:04","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2634"},"modified":"2012-01-05T21:40:04","modified_gmt":"2012-01-06T02:40:04","slug":"javascript-format-a-number-to-display-thousands-us-style","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/javascript-format-a-number-to-display-thousands-us-style\/","title":{"rendered":"javascript: Format a number to display thousands US style (#,###,###,###)"},"content":{"rendered":"<p>[javascript]<br \/>\n\/**<br \/>\n * Format a number to display thousands like in the US -&gt; 1000000 =&gt; 1,000,000<br \/>\n * @param number<br \/>\n * @returns<br \/>\n *\/<br \/>\nfunction formatThousands(number) {<br \/>\n\treturn Math.max(0, number).toFixed(0).replace(\/(?=(?:d{3})+$)(?!^)\/g, &#8216;,&#8217;);<br \/>\n}<br \/>\n[\/javascript] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[javascript] \/** * Format a number to display thousands like in the US -&gt; 1000000 =&gt; 1,000,000 * @param number * @returns *\/ function formatThousands(number) { return Math.max(0, number).toFixed(0).replace(\/(?=(?:d{3})+$)(?!^)\/g, &#8216;,&#8217;); } [\/javascript]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[],"class_list":["post-2634","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Gu","jetpack-related-posts":[{"id":2637,"url":"https:\/\/www.gubatron.com\/blog\/javascript-capitalize-text-like-in-this-title\/","url_meta":{"origin":2634,"position":0},"title":"Javascript: Capitalize Text Like In This Title","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"On this one, we'll show of the dynamic nature of javascript and we're going to make all strings have a new method called \"capitalize()\". So you can do stuff like this: [javascript] console.log(\"this text should look nicer now\".capitalize()); >> This Text Should Look Nicer Now [\/javascript] Here's the magic code\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1535,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-how-many-digits-are-there-in-a-decimal-number\/","url_meta":{"origin":2634,"position":1},"title":"JavaScript: Get how many digits are there in a decimal number","author":"gubatron","date":"January 16, 2010","format":false,"excerpt":"\/** * 1 + Floor(LogBase10(number)) * *\/ function digits(n) { return 1+Math.floor(Math.log(n)\/Math.log(10)); } Yes, javascript doesn't have a Math.log10() function. I tried doing it by dividing by Math.ln10 constant but the function won't return the correct number of digits when you pass numbers like 100,1000,10000,etc. So I just divided by\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2632,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","url_meta":{"origin":2634,"position":2},"title":"javascript: Get N random elements from a List","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Walks linearly through the list to find an element. * returns true if it's found. *\/ function elementIn(collection, element) { for (var i=0; i < collection.length; i++) { if (collection[i]==element) { return true; } } return false; } \/** * Returns a new list of n random\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2671,"url":"https:\/\/www.gubatron.com\/blog\/how-to-shuffle-a-listarray-in-javascript\/","url_meta":{"origin":2634,"position":3},"title":"How to shuffle a List (or Array) in Javascript","author":"gubatron","date":"February 24, 2012","format":false,"excerpt":"[javascript] \/** * Returns number starting from offset up to n-1 *\/ function getRandomWithOffset(n,offset) { return Math.floor(Math.random()*n+offset); } \/** * Returns random integer from 0 to n-1 *\/ function getRandom(n) { return getRandomWithOffset(n,0); } \/** Fisher\u2013Yates shuffle algorithm O(n) *\/ function shuffleList(list) { for (var i=list.length-1; i>0; i--) { var\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3386,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-foreach-function-in-javascript\/","url_meta":{"origin":2634,"position":4},"title":"How to make a &#8220;foreach&#8221; function in JavaScript","author":"gubatron","date":"April 4, 2015","format":false,"excerpt":"I thought this would be a simple exercise in case of having to interview someone for a JavaScript position. \"How would you make your own 'foreach' in JavaScript\" I came up with the following solution: \/\/ \/\/ collection: A list of objects. \/\/ onElementIterationCallback: The function to be called on\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3,"url":"https:\/\/www.gubatron.com\/blog\/javascript-quicksort-implementation-with-dynamic-comparator\/","url_meta":{"origin":2634,"position":5},"title":"Javascript Quicksort implementation with dynamic comparator.","author":"gubatron","date":"February 29, 2012","format":false,"excerpt":"[javascript] Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b]; this[b]=tmp; } function quickSort(array,comparator) { qsort(array,0,array.length,comparator); } \/** * NOTE: the comparator is a dynamic function you will define like so comparator(a,b) { if (a > b) return 1; else if (a < b) return -1; else { return 0; } } *\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2634"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2634\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}