0

I am working on a trigger that sets the price of that a customer pays us. There are requirements that I am struggling with meeting.

If the decimal for the price is lower than .95 then it needs to be rounded up or set to .95, eg $1.50 would be rounded to 1.95.

If the decimal for the price is greater than .95 then it needs to be rounded up to the next 1.95, eg $1.96 would be rounded up to 2.95.

  • 1
    The Apex Decimal class has all the methods you need. – Keith C Aug 21 '18 at 15:58
6

Who needs an if statement:

  • 1.94 + .05 = 1.99 round up to 2.0 - .05 = 1.95
  • 1.95 + .05 = 2.0 Round up to 2.0 - .05 = 1.95 - I ran a test to prove this one out
  • 1.96 + .05 = 2.01 round up to 3.0 - .05 = 2.95
  • 3.35 + .05 = 3.4 round up to 4.0 - .05 = 3.95

In other words:

//Assume your price variable is called price
price += .05;
price = price.round(System.RoundingMode.UP);
price += -.05;

Boredom Update

A gentleman I know pointed out a while ago that you achieve the "normal" rounding behavior in a pretty simple way:

Decimal value += .5
Integer roundedValue = (Integer) value;

I just adopted that methodology to fit your case. The differences are

  1. you always want to round up
  2. your "half-way" point is .95 as opposed to .5.
| improve this answer | |
2

That is not a rounding mode supported by the Decimal class, but it's easy enough to implement in logic.

What you need to do is truncate the value (chop off the fractional amount) and then subtract the result from the value so you get back just the fraction. Then you can easily write logic comparing the fraction to 0.95.

The way to get this fractional value, assuming your dollar value is a Decimal value (as it should be), is to call

myFraction = myDecimal.round(System.RoundingMode.DOWN))

You can implement your logic from there to make adjustments to the final price.

| improve this answer | |
2

Here's a static method you can use to do your custom rounding.

static Decimal roundAmount(Decimal originalAmount, Decimal roundTo){
  Decimal cents = (originalAmount - Math.Floor(originalAmount));
  Decimal dollars = (originalAmount - cents);
  if ( cents > roundTo ){
     dollars++;
  }
  return (dollars+roundTo);
}
| improve this answer | |
2

I have created a static method that will work in your scenario

 public static decimal RoundOf(Decimal value){
    long ceilingValue = value.round(System.RoundingMode.CEILING);
    //Calculate mod value because Math.Mod is not supporting decimal
    decimal divideValue = value / 1;
    decimal c= Math.floor(divideValue);
    decimal modValue = value - (1 * c);
    //end

    if(modValue <= .95){
        return ceilingValue - .05;
    }else{
        return ceilingValue + .95;
    }
}

Example:

RoundOf(.93) // output = .95
RoundOf(.96) // output = 1.95
RoundOf(1.92) // output = 1.95
RoundOf(1.96) // output = 2.95
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.