This content has been marked as final.
Show 2 replies

Re: Rounding an integer to the nearest 100 or 1000
Sanford Whiteman Dec 9, 2018 7:35 PM (in response to Arjun Naskar)1 of 1 people found this helpfulFirst: don't use ${formal} references in function calls. They lead to syntax errors because they can't be chained.
Second: $math.roundTo rounds to decimal places. That's not the same as "closest hundred." (It also doesn't accept negative decimal places.)
Third: because of the oversimplification of $math.round (which always uses HALF_EVEN rounding) and Velocity's rather tragic verbosity you need this:
## supply your field and rounding scale #set( $valueToRound = $lead.Message_Volume__c_contact ) #set( $toNearest = 100 ) ## ## /  begin rounding routine  \ ## No need to alter this code! #set( $String = $context.getClass().forName("java.lang.String") ) #set( $NativeMath = $context.getClass().forName("java.lang.Math") ) #set( $BigDecimal = $context.getClass().forName("java.math.BigDecimal") ) #set( $BigDecimalFromString = $BigDecimal.getConstructor($String) ) #set( $pointsToMove = $NativeMath.log10($toNearest.doubleValue()).intValue() ) #set( $rounded = $BigDecimalFromString.newInstance( $valueToRound.toString() ).movePointLeft( $pointsToMove ).setScale( 0, $field.in($BigDecimal).ROUND_HALF_UP ).movePointRight( $pointsToMove ) ) ## \  end rounding routine  / Rounded: ${rounded}
There are some alternate ways with $math.mod but none are as flexible, nor do they use real mathematical methods.
Fourth: the US locale commas are simple:
#set( $roundedWithCommas = $number.format('#,###', $rounded) ) ${roundedWithCommas}

Re: Rounding an integer to the nearest 100 or 1000
Sanford Whiteman Dec 8, 2018 5:03 PM (in response to Arjun Naskar)Arjun please return to this thread and read the supplied answer.