{"id":1398,"date":"2016-07-15T23:06:09","date_gmt":"2016-07-15T17:06:09","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1398"},"modified":"2017-04-01T02:51:33","modified_gmt":"2017-03-31T20:51:33","slug":"comma-separate-number-javascript-safari-compatiable","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/comma-separate-number-javascript-safari-compatiable\/","title":{"rendered":"Comma Separate Number in Javascript &#8211; Safari Compatiable"},"content":{"rendered":"<h2>JS Comma Separate Number &#8211; Not-Browser Compliant<\/h2>\n<p>In Javascript, it&#8217;s quite simple to convert a straight number to a number with thousands-place commas.<\/p>\n<pre class=\"lang:js decode:true\">var number = 1234;  \/\/ number\r\nnumber.toLocaleString();\r\n\r\n\/\/ Output (Most Browsers):\r\n\/\/ 1,234\r\n\r\n\/\/ Output (Safari):\r\n\/\/ 1234<\/pre>\n<p>And if your number was a string, you can simply do something like this to make it work as well:<\/p>\n<pre class=\"lang:js decode:true\">var number = \"1234\"  \/\/ string\r\nparseInt( number ).toLocaleString();<\/pre>\n<pre class=\"lang:js decode:true\">\/\/ Output (Most Browsers):\r\n\/\/ 1,234\r\n\r\n\/\/ Output (Safari):\r\n\/\/ 1234<\/pre>\n<p>As you can see above though,\u00a0&#8211; it doesn&#8217;t work on Safari web browsers. \u00a0That is true for both desktop and iOS mobile phones\/tablets.<\/p>\n<figure id=\"attachment_1399\" aria-describedby=\"caption-attachment-1399\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma.jpg\" rel=\"attachment wp-att-399\" data-lasso-id=\"617\" data-rel=\"lightbox-gallery-DaCcUnWv\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"size-medium wp-image-1399\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma-500x418.jpg\" alt=\"How to add a comma in the thousands place using Javascript so it's compatible with all browsers - including Safari.\" width=\"500\" height=\"418\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma-500x418.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma-768x642.jpg 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma.jpg 1024w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma-150x125.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma-600x502.jpg 600w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-1399\" class=\"wp-caption-text\">How to add a comma in the thousands place using Javascript so it&#8217;s compatible with all browsers &#8211; including Safari.<\/figcaption><\/figure>\n<h2>JS Comma Separate Number &#8211; Browser Compliant<\/h2>\n<p>To fix this, you need to create a slightly more complex formula, but it can still be done in one line if you&#8217;d like.<\/p>\n<pre class=\"lang:js decode:true\">var number = 1234;  \/\/ number\r\nnumber.toString().replace(\/.(?=(?:.{3})+$)\/g, '$&amp;,')\r\n\r\n\/\/ Output (Most Browsers):\r\n\/\/ 1,234\r\n\r\n\/\/ Output (Safari):\r\n\/\/ 1,234\r\n<\/pre>\n<p>You could also create a function out of this if you&#8217;d like to make it a bit cleaner to reuse.<\/p>\n<pre class=\"lang:js decode:true\">function formatThousands(num) {\r\n    return num.toString().replace(\/.(?=(?:.{3})+$)\/g, '$&amp;,')\r\n}\r\n\r\nvar number = 1234;  \/\/ number\r\nformatThousands(number);\r\n\r\n\/\/ Output (Most Browsers):\r\n\/\/ 1,234\r\n\r\n\/\/ Output (Safari):\r\n\/\/ 1,234<\/pre>\n<p>Lastly, of note &#8211; I didn&#8217;t come up with this magic &#8211; rather am documenting it here so that I (or others) could find this quickly\/easily. \u00a0The source of this solution (and a few other neat tips) can be found on the <a href=\"https:\/\/css-tricks.com\/snippets\/javascript\/comma-values-in-numbers\/\" target=\"_blank\" data-lasso-id=\"618\" rel=\"noopener\">CSS Tricks website<\/a>.<\/p>\n<h2>JS Comma Separate Number &#8211; Browser Compliant Handling Decimals<\/h2>\n<h4>Added 2017-03-31<\/h4>\n<p>One other item of note is that the above function won&#8217;t handle decimal \u00a0numbers. \u00a0One tweak to the function and we can easily accomplish this however.<\/p>\n<pre class=\"lang:js decode:true\">function formatThousands(num) {\r\n    var values = num.toString().split('.');\r\n    return values[0].replace(\/.(?=(?:.{3})+$)\/g, '$&amp;,') + ( values.length == 2 ? '.' + values[1] : '' )\r\n}\r\n\r\nvar number = 1234.56;  \/\/ number\r\nformatThousands(number);\r\n\r\n\/\/ Output (Most Browsers):\r\n\/\/ 1,234.56\r\n\r\n\/\/ Output (Safari):\r\n\/\/ 1,234.56<\/pre>","protected":false},"excerpt":{"rendered":"<p>JS Comma Separate Number &#8211; Not-Browser Compliant In Javascript, it&#8217;s quite simple to convert a straight number to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1399,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[12,5],"tags":[],"class_list":["post-1398","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-jquery","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/07\/Javascript-Safari-Number-Thousands-Separated-By-Comma.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-my","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1398"}],"version-history":[{"count":3,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1398\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1398\/revisions\/1480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1399"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}