Posted: 30 Aug 2017 9:59 EDT Last activity: 6 Sep 2017 5:09 EDT
Truncating Decimal places without doing a rounding off
We have a requirement where we have to limit the no of decimal places to 2. When the user enters the value in any amount fields, the entered value should be formatted in the proper decimal format as below.
1. If entered 12345 change it to 12345.00
2. If entered 12345.234 change it to 12345.23
3. If entered 12345.235 change it to 12345.23
We are using the Decimal control in which we have mentioned the Precision as 2 and we are refreshing the screen on change. When the user enters the value and Tab out, the first two scenarios works fine. But the third scenario is doing a round off and changes the value to 12345.24. As this is an amount field the rounding off is not required. Please let us know how we can achieve this.
Thanks for the reply. I tried this divide function and it worked. But now i am able to use this in a DataTransform which is called on refresh of that amount field's OnChange. So i have created a DataTransform for each of the amount fields. Is there a way i can use this function in a control so that it will be applicable to all the amount fields easily.
I fixed it by customizing the OOTB Decimal control. In that control the formatting is done by calling the method PRNumberFormat.format(null, PRNumberFormat.DEFAULT_DECIMAL, bGrouping, null, iPrecision, value). As per that if there is more decimal places than the value specified in the Precision param, it will be rounded off to the mentioned no.of digits. This method will do the general number format validation, Negative number validation,add seperators and format the decimal places. We have to customize only the decimal formatting. So i have added a line to call the method "BigDecimal newValue = value.setScale(iPrecision, BigDecimal.ROUND_DOWN);" just above the the PRNumberFormat method and passed the 'newValue' variable in the place of 'value'. So that the decimal value will be truncated without rounding off, before calling the PRNumberFormat method. As the decimal value is now already truncated to the required length, the PRNumberFormat method will do only general number format validation, Negative number validation and adding seperators.