{"id":767,"date":"2010-05-21T16:08:49","date_gmt":"2010-05-21T10:38:49","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=767"},"modified":"2016-12-19T15:05:48","modified_gmt":"2016-12-19T09:35:48","slug":"injecting-method-to-convert-string-number-to-number-having-brackets","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/","title":{"rendered":"Injecting method to convert String number to number (Having , $ brackets)"},"content":{"rendered":"<p>Recently in my project we have use case where users have the habit of entering number as 100,000 and they write the -ve numbers as (100,000). Some times they also pur $ with the number field<br \/>\nInitially when we were not aware of this we were populating the object like<\/p>\n<pre>\r\nExpense expense =new Expense(params)\r\n<\/pre>\n<p>This throw exception as the user type &#8220;,&#8221; in the number then we moved to <\/p>\n<pre>\r\nbindData(expense,params)\r\n<\/pre>\n<p>This handles the number with &#8220;,&#8221; very gracefully.<br \/>\nNow the real use case came into picture where we need to ignore $ and change the number enclosed with brackets to -ve number. We dont want to change our code for this so we came up with the solution of injecting a method in String class which will do all the things for us and we added a method in the project bootstrap which is something like . <\/p>\n<pre>\r\nString.metaClass.convertToNumber = {\r\n            String stringNumber = delegate?.toString()\r\n            stringNumber = stringNumber.replaceAll(\"[,]|[(]|[)]|[\\$]\",'')\r\n            Float number = 0\r\n            if (stringNumber.isNumber()) {\r\n                number = Float.parseFloat(stringNumber)\r\n                if (delegate?.toString()?.endsWith(\")\")) {\r\n                    number = -number\r\n                }\r\n            }\r\n            return number\r\n        }\r\n<\/pre>\n<p>And we added the line in the Application Filter <\/p>\n<pre>\r\nparams?.totalCost = params?.totalCost?.convertToNumber()\r\n<\/pre>\n<p>It just do the trick for us and we dont need to change our code. I think its one of the correct way to handle such requirement <\/p>\n<p><\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><br \/>\n<a href=\"https:\/\/twitter.com\/farjinaarad\">https:\/\/twitter.com\/meudaypratap<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in my project we have use case where users have the habit of entering number as 100,000 and they write the -ve numbers as (100,000). Some times they also pur $ with the number field Initially when we were not aware of this we were populating the object like Expense expense =new Expense(params) This [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[244,242],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/767"}],"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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}