+ Start a Discussion
Ross.ax1563Ross.ax1563 

Rounding to the nearest 50

Hi All

 

I do I round a decimal value to the nearest 50 (in apex)?

 

Many thanks

Ross

ibtesamibtesam

Hi,

Use

 

Math.round(Number * decPlace) / decPlace;

where decPlace is number of decimal places u want to round.

kiranmutturukiranmutturu

if that varaible is decimal or double ,, you can make use of rouding mode functoionality  check it out this

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_decimal.htm#apex_decimal_rounding_mode

sfdcfoxsfdcfox
If I take your question literally, it looks like you want to find the nearest multiple of 50? You can do this:

Decimal result = origin - math.mod(origin + 25, 50);

To get the nearest 0.50, You will just need to adjust this slightly differently:

Decimal result = origin - (math.mod((origin+0.25)*100,100)/100);

Note that this method is roughly equivalent to ibtesam's answer, but demonstrates how to round to any arbitrary decimal place or whole integer.