2

I was trying to use math.mod(decimal,decimal) but I get the following error: Method does not exist or incorrect signature: void mod(Decimal, Decimal) from the type System.Math I know it is easy to make my own but it seems odd.

Update:

Here is what I implemented to account for lack of support.

    public static Decimal decimalMod(Decimal x, Decimal y){

    Decimal remainder = 0;

    if(x != 0){
        Decimal firstResult = x / y; 
        Decimal flooredResult = math.floor(firstResult);
        Decimal flooredProduct = flooredResult * y;
        remainder = x - flooredProduct; 
    }

    return remainder;
}

// example: decimalMod(5.01, 0.25) would result in a remainder of .01.
2

As of Spring 19 (and all prior versions), we only have mod(Integer, Integer) and mod(Long, Long). There is no known information of when, or if, we might get a built in for Decimal or Double values.

2

In case anyone happens upon this page, one way to do this is (assuming someDecimal is non-null):

Decimal remainder = someDecimal - someDecimal.round(RoundingMode.DOWN);

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.