{"id":2585,"date":"2020-05-10T06:04:53","date_gmt":"2020-05-10T03:04:53","guid":{"rendered":"https:\/\/elzero.org\/?p=2585"},"modified":"2022-08-07T06:48:10","modified_gmt":"2022-08-07T03:48:10","slug":"python-problems-solving-add-commas-and-underscores","status":"publish","type":"post","link":"https:\/\/elzero.org\/old\/python-problems-solving-add-commas-and-underscores\/","title":{"rendered":"Add Commas And Underscores To Number"},"content":{"rendered":"<h3>\u0627\u0644\u062d\u0644 \u0627\u0644\u0645\u0648\u062c\u0648\u062f \u0641\u064a \u0627\u0644\u0641\u064a\u062f\u064a\u0648<\/h3>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\ndef add_commas_and_underscore(num):\r\n\r\n  # Use Commas As Thousands Separator\r\n  # formatted_num = \"{:,}\".format(num) # Old Way\r\n  formatted_num = f\"{num:,}\"  # New Way\r\n\r\n  # Check if Number > 3\r\n\r\n  if len(formatted_num) > 3:\r\n\r\n    # Convert To List\r\n    to_list = formatted_num.split(\",\")\r\n\r\n    # Get First Part\r\n    first = to_list[:-1]\r\n\r\n    # Join First Part Element\r\n    first_join = \"\".join(first)\r\n\r\n    # Convert To Int\r\n    first_int = int(first_join)\r\n\r\n    # Formatting The First Part\r\n    first_format = f\"{first_int:,}\"\r\n\r\n    # Concatenate All\r\n    final_result = f\"{first_format}_{to_list[-1]}\"\r\n\r\n    return final_result\r\n\r\n  return formatted_num\r\n\r\nprint(add_commas_and_underscore(120)) # 120\r\nprint(add_commas_and_underscore(1530)) # 1_530\r\nprint(add_commas_and_underscore(120510650)) # 120,510_650\r\nprint(add_commas_and_underscore(510650480910)) # 510,650,780_910\r\nprint(add_commas_and_underscore(12069057014032)) # 12,069,057,014_032\r\n<\/script>\r\n\r\n<hr class=\"separator\">\r\n\r\n<h3>\u0627\u0644\u062d\u0644 \u0627\u0644\u062b\u0627\u0646\u064a<\/h3>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\ndef add_commas_and_underscore(num):\r\n\r\n  # Use Commas As Thousands Separator\r\n  num = f\"{num:,}\"\r\n\r\n  # Check If Theres Comma in The Position 4 From The Right\r\n  if (num[-4:-3] == \",\"):\r\n\r\n    # Replace Last Comma With Underscore\r\n    num = f\"{num[:-4]}_{num[-3:]}\"\r\n\r\n  return num\r\n<\/script>","protected":false},"excerpt":{"rendered":"<p>Add Commas And Underscores To Number With Python<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1337],"tags":[],"class_list":["post-2585","post","type-post","status-publish","format-standard","hentry","category-problems-solving"],"_links":{"self":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/2585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/comments?post=2585"}],"version-history":[{"count":9,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/2585\/revisions"}],"predecessor-version":[{"id":8540,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/2585\/revisions\/8540"}],"wp:attachment":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/media?parent=2585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/categories?post=2585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/tags?post=2585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}