{"id":10650,"date":"2012-10-08T23:16:00","date_gmt":"2012-10-08T22:16:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=5e9e63f258842c4b828098ba4bfbc90c"},"modified":"2022-08-02T15:25:26","modified_gmt":"2022-08-02T13:25:26","slug":"bigdecimal-and-locale-in-grails","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2012\/10\/08\/bigdecimal-and-locale-in-grails\/","title":{"rendered":"BigDecimal and Locale in Grails"},"content":{"rendered":"<p>I was recently puzzled by strange\u00a0behaviour\u00a0of Grails application with web service interface. It resulted with rounding currency amount values when I sent request from my browser but it worked perfectly if another client application sent same request. After investigation it turned out that the HTTP requests were not exactly identical. The browser request contained header entry with Polish locale pl-PL for which coma is a decimal separator.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Request\r\nhttp:\/\/localhost:8080\/helloworld\/amount\/displayAmount?amount=12.22<\/pre>\n<p>Header<br \/>\nAccept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4<\/p>\n<p>In order to reproduce this behaviour I created a simple Grails 2.1 HelloWorld app.<\/p>\n<p>AccountController.groovy<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">package helloworld\r\n\r\nclass AmountController {\r\n\r\n    def displayAmount(PaymentData paymentData) {\r\n        render \"Hello, this is your amount: \" + paymentData.amount.toString()\r\n    }\r\n}\r\n\r\nPaymentData.groovy:\r\n\r\n    package helloworld\r\n\r\nimport grails.validation.Validateable\r\n\r\n@Validateable\r\nclass PaymentData {\r\n    BigDecimal amount\r\n\r\n    static constraints = {\r\n        amount(nullable: false, min: BigDecimal.ZERO, scale: 2)\r\n    }\r\n}<\/pre>\n<p>\u00a0<\/p>\n<p>Starting this app we can observe rounding of cents in amount to 00 when sending request with dot in amount (<a href=\"http:\/\/localhost:8080\/helloworld\/amount\/displayAmount?amount=12.22\">http:\/\/localhost:8080\/helloworld\/amount\/displayAmount?amount=12.22<\/a>)<\/p>\n<p><a href=\"http:\/\/4.bp.blogspot.com\/-HpL9C9bNj7g\/UHNLQQ4Vc8I\/AAAAAAAAFqI\/U-9tcbLfZ8k\/s1600\/Zrzut+ekranu+z+2012-10-08+23%3A18%3A06.png\"><img decoding=\"async\" src=\"http:\/\/4.bp.blogspot.com\/-HpL9C9bNj7g\/UHNLQQ4Vc8I\/AAAAAAAAFqI\/U-9tcbLfZ8k\/s320\/Zrzut+ekranu+z+2012-10-08+23%3A18%3A06.png\" border=\"0\" \/><\/a><br \/>\nWhereas for amount with coma it gives a result with valid cent part (<a href=\"http:\/\/localhost:8080\/helloworld\/amount\/displayAmount?amount=12.22\">http:\/\/localhost:8080\/helloworld\/amount\/displayAmount?amount=12,22<\/a>)<\/p>\n<p><a href=\"http:\/\/3.bp.blogspot.com\/-6bspzZctsn8\/UHNLSs9tX9I\/AAAAAAAAFqQ\/U-yLIy881vQ\/s1600\/Zrzut+ekranu+z+2012-10-08+23%3A18%3A36.png\"><img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/-6bspzZctsn8\/UHNLSs9tX9I\/AAAAAAAAFqQ\/U-yLIy881vQ\/s320\/Zrzut+ekranu+z+2012-10-08+23%3A18%3A36.png\" border=\"0\" \/><\/a><br \/>\nSame behaviour might be expected in case of other locales that use coma as decimal separator, e.g. de_DE. Very not an obvious feature.<\/p>\n","protected":false},"excerpt":{"rendered":"I was recently puzzled by strange&nbsp;behaviour&nbsp;of Grails application with web service interface. It resulted with rounding currency amount values when I sent request from my browser but it worked perfectly if another client application sent same&#8230;\n","protected":false},"author":44,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50,68],"class_list":{"0":"post-10650","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-groovy","8":"tag-java"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/10650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=10650"}],"version-history":[{"count":6,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/10650\/revisions"}],"predecessor-version":[{"id":14876,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/10650\/revisions\/14876"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=10650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=10650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=10650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}