SOLVED

Rounding numbers

Go to solution
Christin_Scholz
Level 2

Rounding numbers

Hi all,

how can I format a number in lead database like 58.5123434565 into 58,51 within the email?

I want to use it as currency in emails with comma (german way of writing).

I tried with the script token, but apparently it didn't work.

$math.roundTo( x, y )

Can you guys please help me on that? (I'm not a developer )

Best
Christin

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Rounding numbers

You didn't specify banker's rounding or arithmetic rounding.

 

Either way you need a locale-specific format for Germany (de_DE).

 

Banker's rounding is the default mode (note this not a Marketo-specific thing, nor a Velocity thing, but across anything built on the Java foundation):

 

#set( $customFormat = $number.getNumberFormat("${esc.h}${esc.h},${esc.h}${esc.h}${esc.h}.00", $convert.toLocale("de_DE") ) )
banker's: ${customFormat.format($lead.SomeField)}‍‍‍‍‍‍

 

 

If you need arithmetic rounding (if neither you nor your manager is a developer or accountant, you probably meant arithmetic):

 

#set( $customFormat = $number.getNumberFormat("${esc.h}${esc.h},${esc.h}${esc.h}${esc.h}.00", $convert.toLocale("de_DE") ) )
#set( $void = $customFormat.setRoundingMode($field.in($customFormat.getRoundingMode()).HALF_UP) )
arithmetic: ${customFormat.format($lead.SomeField)}‍‍‍‍‍‍‍‍‍

 

 

Update 2019-12-07: please read responses to your threads, it's very frustrating to take time to provide Correct answers and have the thread abandoned.

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Rounding numbers

You didn't specify banker's rounding or arithmetic rounding.

 

Either way you need a locale-specific format for Germany (de_DE).

 

Banker's rounding is the default mode (note this not a Marketo-specific thing, nor a Velocity thing, but across anything built on the Java foundation):

 

#set( $customFormat = $number.getNumberFormat("${esc.h}${esc.h},${esc.h}${esc.h}${esc.h}.00", $convert.toLocale("de_DE") ) )
banker's: ${customFormat.format($lead.SomeField)}‍‍‍‍‍‍

 

 

If you need arithmetic rounding (if neither you nor your manager is a developer or accountant, you probably meant arithmetic):

 

#set( $customFormat = $number.getNumberFormat("${esc.h}${esc.h},${esc.h}${esc.h}${esc.h}.00", $convert.toLocale("de_DE") ) )
#set( $void = $customFormat.setRoundingMode($field.in($customFormat.getRoundingMode()).HALF_UP) )
arithmetic: ${customFormat.format($lead.SomeField)}‍‍‍‍‍‍‍‍‍

 

 

Update 2019-12-07: please read responses to your threads, it's very frustrating to take time to provide Correct answers and have the thread abandoned.

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Rounding numbers

 please return to your thread. The correct answer was given within hours of your post.

Christin_Scholz
Level 2

Re: Rounding numbers

I'm sorry, I was sick.

Thank you very much for your answer!

SanfordWhiteman
Level 10 - Community Moderator

Re: Rounding numbers

Thanks for checking back, I appreciate it.

Austin_Whiting1
Level 1

Re: Rounding numbers

Hi there, 

This is very helpful for an issue I am having, I think it is just about what I need. I have a "float" field which I need to round to the nearest whole number. Can I use some aspect of this to get there? 

 

I am trying to use a Script Token  as such: 

#set( $customFormat = $number.getNumberFormat("${esc.h}${esc.h},${esc.h}${esc.h}${esc.h}.00", $convert.toLocale("de_DE") ) )
banker's: ${customFormat.format(${lead.lMavgcallduration})}

 

However, I am getting this error when I try to send myself a test: 

 

An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?!

Invocation of method 'format' in class java.text.DecimalFormat threw exception java.lang.IllegalArgumentException: Cannot format given Object as a Number near ?

 

Any ideas on what my issue might be? 

 

Thanks a ton, 
Austin

SanfordWhiteman
Level 10 - Community Moderator

Re: Rounding numbers

Try to use a real email, not a sample. (In general, Velocity shouldn't be tested using Send Sample.)