{"id":471,"date":"2013-11-08T10:50:39","date_gmt":"2013-11-08T09:50:39","guid":{"rendered":"http:\/\/www.netnea.com\/cms\/?p=471"},"modified":"2013-11-08T10:51:16","modified_gmt":"2013-11-08T09:51:16","slug":"thousands-separators-for-numbers-in-python-2-6","status":"publish","type":"post","link":"https:\/\/www.netnea.com\/cms\/2013\/11\/08\/thousands-separators-for-numbers-in-python-2-6\/","title":{"rendered":"Thousands separators for numbers in Python < 2.6"},"content":{"rendered":"<p>Python 2.7 and later have format(), but in Python 2.6 and lower, if you want to format big numbers with thousands separators, you are on your own. This code seem to work nicely and is tolerant.<!--more--><\/p>\n<pre># ---------------------------------------------------------------------------------------\r\ndef format_nr(number):\r\n# 1000000000 --&gt; 1'000'000'000\r\n# Python 2.7 has formats, but we are still under 2.6\r\n# ---------------------------------------------------------------------------------------\r\n\r\n    try:\r\n        number = int(number)        \r\n        s = '%d' % number\r\n        groups = []\r\n        while s and s[-1].isdigit():\r\n            groups.append(s[-3:])\r\n            s = s[:-3]\r\n        return s + \"'\".join(reversed(groups))\r\n\r\n    except:\r\n        return number<\/pre>\n<p>print format_nr(1000000000)<br \/>\n1&#8217;000&#8217;000&#8217;000<\/p>\n<p>The quote separator is common in Europe. US usually wants comma.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 2.7 and later have format(), but in Python 2.6 and lower, if you want to format big numbers with thousands separators, you are on your own. This code seem to work nicely and is tolerant.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":{"0":"post-471","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-nms","7":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/comments?post=471"}],"version-history":[{"count":3,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":474,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/471\/revisions\/474"}],"wp:attachment":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}