SOLVED
## Rounding an integer to the nearest 100 or 1000

Go to solution

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-05-2018
02:58 PM

12-05-2018
02:58 PM

We have an attribute that represents the number of messages a particular user has sent. Because it seems weird to list the exact number, we want to round this to the closest hundred value. I assume velocity is the way to go and came up with the following but it failed to populate (just remained blank in the sample email send with a confirmed non null value)

#set ($rounded_number = $math.roundTo(2, ${lead.Message_Volume__c_contact}))

Additionally, over tens of thousands of messages, it would be helpful to have the usual (US format) commas in place for sets of 3 orders of magnitude.

Any advice or direction would be much appreciated.

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-05-2018
07:19 PM

12-05-2018
07:19 PM

First: 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 th*e* 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}

4 REPLIES 4

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-05-2018
07:19 PM

12-05-2018
07:19 PM

First: 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 th*e* 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}

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-08-2018
05:03 PM

12-08-2018
05:03 PM

Re: Rounding an integer to the nearest 100 or 1000

Arjun Naskar please return to this thread and read the supplied answer.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-27-2018
12:46 PM

12-27-2018
12:46 PM

Re: Rounding an integer to the nearest 100 or 1000

Again Arjun Naskar please return to this thread and read the supplied answer.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-03-2019
01:40 PM

01-03-2019
01:40 PM

Re: Rounding an integer to the nearest 100 or 1000

Very helpful. Thank you!