{"id":1535,"date":"2010-01-16T14:01:58","date_gmt":"2010-01-16T19:01:58","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/2010\/01\/16\/javascript-get-how-many-digits-are-there-in-a-decimal-number\/"},"modified":"2010-01-16T14:01:58","modified_gmt":"2010-01-16T19:01:58","slug":"javascript-get-how-many-digits-are-there-in-a-decimal-number","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/javascript-get-how-many-digits-are-there-in-a-decimal-number\/","title":{"rendered":"JavaScript: Get how many digits are there in a decimal number"},"content":{"rendered":"<pre>\n\/**\n * 1 + Floor(LogBase10(number))\n * \n*\/\nfunction digits(n) { return 1+Math.floor(Math.log(n)\/Math.log(10)); \n}\n<\/pre>\n<p>Yes, javascript doesn&#8217;t have a Math.log10() function.<br \/>\nI tried doing it by dividing by Math.ln10 constant but the function won&#8217;t return the correct number of digits when you pass numbers like 100,1000,10000,etc. So I just divided by Math.log(10) and it seems to work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/** * 1 + Floor(LogBase10(number)) * *\/ function digits(n) { return 1+Math.floor(Math.log(n)\/Math.log(10)); } Yes, javascript doesn&#8217;t have a Math.log10() function. I tried doing it by dividing by Math.ln10 constant but the function won&#8217;t return the correct number of digits when you pass numbers like 100,1000,10000,etc. So I just divided by Math.log(10) and it seems to [&hellip;]<\/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":[30],"tags":[585],"class_list":["post-1535","post","type-post","status-publish","format-standard","hentry","category-geeklife","tag-javascript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-oL","jetpack-related-posts":[{"id":2632,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","url_meta":{"origin":1535,"position":0},"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":1535,"position":1},"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":3,"url":"https:\/\/www.gubatron.com\/blog\/javascript-quicksort-implementation-with-dynamic-comparator\/","url_meta":{"origin":1535,"position":2},"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":[]},{"id":2637,"url":"https:\/\/www.gubatron.com\/blog\/javascript-capitalize-text-like-in-this-title\/","url_meta":{"origin":1535,"position":3},"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":2233,"url":"https:\/\/www.gubatron.com\/blog\/twitterautolinks-jquery-extension-to-replace-twitter-names-for-links-to-their-respective-pages\/","url_meta":{"origin":1535,"position":4},"title":"twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.","author":"gubatron","date":"August 2, 2011","format":false,"excerpt":"Save the following on a .js file, preferably named jquery.twitterAutoLinks.js [javascript] \/** Auto replace all Twitter nicknames for links *\/ $.fn.twitterAutoLinks = function() { return this.each(function() { var html = $(this).html(); $(this).html(html.replace(\/B@([w-]+)\/gm, '<a href=\"http:\/\/twitter.com\/$1\" target=\"_blank\">@$1<\/a>')); }); }; [\/javascript] Usage After importing the jquery.twitterAutoLinks.js file (make sure it's after you've imported\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2634,"url":"https:\/\/www.gubatron.com\/blog\/javascript-format-a-number-to-display-thousands-us-style\/","url_meta":{"origin":1535,"position":5},"title":"javascript: Format a number to display thousands US style (#,###,###,###)","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Format a number to display thousands like in the US -> 1000000 => 1,000,000 * @param number * @returns *\/ function formatThousands(number) { return Math.max(0, number).toFixed(0).replace(\/(?=(?:d{3})+$)(?!^)\/g, ','); } [\/javascript]","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\/1535","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=1535"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1535\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=1535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=1535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=1535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}