A very quick article on a cool snippet of code, another regular expression, I've been trying out to round up a number or at least to remove the trailing zeros. I could have added this to my Zoho Deluge - Some Useful Regular Expressions list but I felt this deserved its own article.
Why?
A client wanted the discount displayed on a template and if they gave 10% discount, it would display as 10.00%. And they need the decimal because sometimes they might offer 12.5%. But when it displays in a template, it isn't that pretty to look at: Instead, could they have 10% to display instead of 10.00%, and display 12.5% instead of 12.50%.
Well there might be a longer solution without using a regular expression (regex), as in check for the decimal point, check each digit thereafter to see if there are zeros... But I'm keen on avoiding using loops where possible so we don't breach a statement execution limit in the Zoho app function.
How?
If we're not going to use loops, then the next best thing I'm aware of is a regular expression.
So the first expression I came up with worked to a certain point but didn't round up the numbers, in other words, it didn't remove the unnecessary zeros. Note that in the following regex, and if using Zoho Creator, you need to replace the double-backslash with a single backslash:
v_Test1 = 12.300; info v_Test1.toString().replaceAll("(\\.\\d*?[1-9])0+$", "$1", false); // yields 12.3 // same expression again but with a different value v_Test2 = 12.000; info v_Test2.toString().replaceAll("(\\.\\d*?[1-9])0+$", "$1", false); // yields 12.000
- v_Test1 = 12.300;
- info v_Test1.toString().replaceAll("(\\.\\d*?[1-9])0+$", "$1", false);
- // yields 12.3
- // same expression again but with a different value
- v_Test2 = 12.000;
- info v_Test2.toString().replaceAll("(\\.\\d*?[1-9])0+$", "$1", false);
- // yields 12.000
So this isn't quite right, so let's get a regex that rounds numbers up but pays attention to where the decimal point is:
v_Test3 = 12.000; info v_Test3.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false); // yields 12 v_Test4 = 12.300; info v_Test4.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false); // yields 12.3 v_Test5 = 12.345; info v_Test5.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false); // yields 12.345 v_Test6 = 120.000; info v_Test6.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false); // yields 120 v_Test7 = -123.4567; info v_Test7.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false); // yields -123.4567
- v_Test3 = 12.000;
- info v_Test3.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false);
- // yields 12
- v_Test4 = 12.300;
- info v_Test4.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false);
- // yields 12.3
- v_Test5 = 12.345;
- info v_Test5.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false);
- // yields 12.345
- v_Test6 = 120.000;
- info v_Test6.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false);
- // yields 120
- v_Test7 = -123.4567;
- info v_Test7.toString().replaceAll("^(^-?\\d+\\.\\d*[1-9])(0+$)|(\\.0+$)", "$1", false);
- // yields -123.4567
Awesome!
Recap on the CRM and Creator version:
v_Test8 = 12.000; info v_Test8.toString().replaceAll("^(^-?\d+\.\d*[1-9])(0+$)|(\.0+$)", "$1", false); // yields 12
- v_Test8 = 12.000;
- info v_Test8.toString().replaceAll("^(^-?\d+\.\d*[1-9])(0+$)|(\.0+$)", "$1", false);
- // yields 12
Additional Note(s):
You might ask why not use the built-in round() function? Well I admit that round() needs to be used for currencies. The issue is that round() only rounds up decimals to integers when the datatype is an integer and not a decimal:
// syntax: // v_Test9.round(2); // round(v_Test9, 2); v_Test9 = 10.0; info v_Test9.round(2); // yields 10.00 v_Test10 = 10; info v_Test10.round(2); // yields 10 v_Test11 = 10.1; info v_Test11.round(2); // yields 10.10
- // syntax:
- // v_Test9.round(2);
- // round(v_Test9, 2);
- v_Test9 = 10.0;
- info v_Test9.round(2);
- // yields 10.00
- v_Test10 = 10;
- info v_Test10.round(2);
- // yields 10
- v_Test11 = 10.1;
- info v_Test11.round(2);
- // yields 10.10
However the opposite can be done as long as you set the datatype to a decimal first and then use the rounding. In other words the following will force the number to have 2 decimal places:
v_Test1 = 12; info v_Test1.toDecimal().round(2); // yields 12.00 v_Test2 = 12.0; info v_Test2.toDecimal().round(2); // yields 12.00 v_Test3 = 12.000; info v_Test3.toDecimal().round(2); // yields 12.00 v_Test4 = 12.300; info v_Test4.toDecimal().round(2); // yields 12.30 v_Test5 = 12.345; info v_Test5.toDecimal().round(2); // yields 12.35 v_Test6 = 120.000; info v_Test6.toDecimal().round(2); // yields 120.00 v_Test7 = -123.4567; info v_Test7.toDecimal().round(2); // yields -123.46
- v_Test1 = 12;
- info v_Test1.toDecimal().round(2);
- // yields 12.00
- v_Test2 = 12.0;
- info v_Test2.toDecimal().round(2);
- // yields 12.00
- v_Test3 = 12.000;
- info v_Test3.toDecimal().round(2);
- // yields 12.00
- v_Test4 = 12.300;
- info v_Test4.toDecimal().round(2);
- // yields 12.30
- v_Test5 = 12.345;
- info v_Test5.toDecimal().round(2);
- // yields 12.35
- v_Test6 = 120.000;
- info v_Test6.toDecimal().round(2);
- // yields 120.00
- v_Test7 = -123.4567;
- info v_Test7.toDecimal().round(2);
- // yields -123.46
- StackOverflow: Using RegEx how do I remove the trailing zeros from a decimal number
- Zoho Deluge - Tryout
- Regex Tester