{"id":2380,"date":"2010-12-14T11:54:21","date_gmt":"2010-12-14T06:24:21","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2380"},"modified":"2010-12-14T11:54:21","modified_gmt":"2010-12-14T06:24:21","slug":"number-formating-using-regular-expression-and-format-method-of-string-class","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/number-formating-using-regular-expression-and-format-method-of-string-class\/","title":{"rendered":"Number Formating using regular expression and format method of String class"},"content":{"rendered":"<p>Hey guys, I found something very useful, so sharing it here.<br \/>\nIn one of my project there was a requirement to create a fixed length user-id. Let&#8217;s say it was 5 characters ID.<br \/>\n<br \/>\nSo in other words <strong>we required to convert <\/strong>&#8211;<br \/>\n&#8216;123&#8217; to &#8216;00123&#8217; and &#8216;1000&#8217; to &#8216;01000&#8217; and &#8216;12345&#8217; will remain &#8216;12345&#8217;.<br \/>\nIt appears very simple, but the thing is <strong>how quick we do this<\/strong>.<br \/>\n<br \/>\nEarlier I thought to do this by calculating the length of id and prefixing required number of zeros.<br \/>\n<br \/>\nAlternate single-line and faster solution is &#8211;<br \/>\n[groovy]<br \/>\n      \/\/ following will return a String with size of 5 characters and prefixes zeros (if id has digits lesser than 5).<br \/>\n         String.format(&#8216;%05d&#8217;, id)<br \/>\n      \/\/ Note down, id is a digit here.<br \/>\n[\/groovy]<\/p>\n<p>That&#8217;s all. . isn&#8217;t it nice?. We can utilize it at many places like &#8211; converting date from &#8216;1-1-2010&#8242; to &#8217;01-01-2010&#8217;.<br \/>\nIdea is to introduce such kind of utility. If you already knew this, please ignore \ud83d\ude42<br \/>\nplease open the ideas &#8211; if you know something better than this.<br \/>\n<br \/>\ncheers!<br \/>\nsalil at IntelliGrape dot com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey guys, I found something very useful, so sharing it here. In one of my project there was a requirement to create a fixed length user-id. Let&#8217;s say it was 5 characters ID. So in other words we required to convert &#8211; &#8216;123&#8217; to &#8216;00123&#8217; and &#8216;1000&#8217; to &#8216;01000&#8217; and &#8216;12345&#8217; will remain &#8216;12345&#8217;. It [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2380"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2380"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2380\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}