Why doesn't the below just display zero? Instead I'm getting 0E-8

    Decimal spend = 0.00;
    Decimal convertType = 1.000000;
    system.debug('Calculate: ' +  spend * convertType);

USER_DEBUG [94]|DEBUG|Calculate: 0E-8


What's happening here is that the scale of two Decimal values is additive when you multiply them. So you take a Decimal with scale 2 and a Decimal with scale 6, you end up with a Decimal of scale 8.

If you call setScale you can get a more normal result. Note, however, that 0E-8 == 0 is still true.

Decimal spend = 0.00;
Decimal conversionRate = 1.000000;
Decimal value = spend * conversionRate;
system.debug(value == 0); // outputs true
system.debug(value.scale()); // outputs 8
system.debug(value.setScale(2)); // outputs 0.00
| 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.