{"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":1,"footnotes":""},"categories":[7],"tags":[244,242],"class_list":["post-767","post","type-post","status-publish","format-standard","hentry","category-grails","tag-convert-string-to-number","tag-groovy-metaprogramming"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Uday Pratap Singh\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#article\",\"name\":\"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog\",\"headline\":\"Injecting method to convert String number to number (Having , $ brackets)\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2010-05-21T16:08:49+05:30\",\"dateModified\":\"2016-12-19T15:05:48+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#webpage\"},\"articleSection\":\"Grails, convert string to number, groovy metaprogramming\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#listItem\",\"name\":\"Injecting method to convert String number to number (Having , $ brackets)\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#listItem\",\"position\":3,\"name\":\"Injecting method to convert String number to number (Having , $ brackets)\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/\",\"name\":\"Uday Pratap Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/d4c98503-ff55-4112-8fc2-e3c4fcc6fc3a_282-Uday-Pratap-Singh-PROFILEPICTURE.jpeg\",\"width\":96,\"height\":96,\"caption\":\"Uday Pratap Singh\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/\",\"name\":\"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog\",\"description\":\"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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/injecting-method-to-convert-string-number-to-number-having-brackets\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/uday\\\/#author\"},\"datePublished\":\"2010-05-21T16:08:49+05:30\",\"dateModified\":\"2016-12-19T15:05:48+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog","description":"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","canonical_url":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#article","name":"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog","headline":"Injecting method to convert String number to number (Having , $ brackets)","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/uday\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2010-05-21T16:08:49+05:30","dateModified":"2016-12-19T15:05:48+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#webpage"},"articleSection":"Grails, convert string to number, groovy metaprogramming"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/www.tothenew.com\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#listItem","name":"Injecting method to convert String number to number (Having , $ brackets)"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#listItem","position":3,"name":"Injecting method to convert String number to number (Having , $ brackets)","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/uday\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/uday\/","name":"Uday Pratap Singh","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/d4c98503-ff55-4112-8fc2-e3c4fcc6fc3a_282-Uday-Pratap-Singh-PROFILEPICTURE.jpeg","width":96,"height":96,"caption":"Uday Pratap Singh"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/","name":"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog","description":"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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/uday\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/uday\/#author"},"datePublished":"2010-05-21T16:08:49+05:30","dateModified":"2016-12-19T15:05:48+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog","og:description":"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","og:url":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Injecting method to convert String number to number (Having , $ brackets) | TO THE NEW Blog","twitter:description":"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","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"767","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:16:02","updated":"2024-02-29 11:00:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tInjecting method to convert String number to number (Having , $ brackets)\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Grails","link":"https:\/\/www.tothenew.com\/blog\/category\/grails\/"},{"label":"Injecting method to convert String number to number (Having , $ brackets)","link":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-convert-string-number-to-number-having-brackets\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"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}]}}