Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
james2000

# Apex Math Bug?

I've run into a disconcerting issue while writing a test case: there seems to be at least one case of decimal division that doesn't produce an accurate result. Since we deal with a lot financial/currency calculations, this is a little scary to me. Has anyone seen anything like this before or can anyone tell me if I'm doing something wrong here:

Decimal num = 2.10;Decimal denom = 2.0;Decimal result = num / denom;System.debug('Result=' + result.setScale(2).toPlainString());if (result != 1.05) System.debug('Not equal!');

If I run this code in an anonymous block, it displays the following:

20090626192509.249:AnonymousBlock.appirio_sm1: line 4, column 1: Result=1.00

20090626192509.249:AnonymousBlock.appirio_sm1: line 6, column 2: Not equal!

Thoughts?

Best Answer chosen by Admin (Salesforce Developers)
JimRae

If you use the decimal divide method, with a scale set, the result is correct:

Decimal num = 2.10;
Decimal denom = 2.0;
Decimal result = num / denom;
system.debug('\n\nresult='+result);
Decimal resultdiv =num.divide(denom,2);
system.debug('\n\ndivtest'+resultdiv);
System.debug('Result2=' + result.setScale(2).toPlainString());
system.debug('\n\n');
if (result != 1.05){
System.debug('Not equal!');
}

if (resultdiv != 1.05){
System.debug('Divide Method Not equal!');
}

Additionally, the documention indicates the "/" symbol is used on an integer or a double.  If you do your calculation with a double, the answer also comes out correct.

/ Division operator. Divides x, an Integer or Double, by y, another Integer or Double. Note that if a double is used, the result is a Double.

Message Edited by JimRae on 06-26-2009 04:51 PM

JimRae

If you use the decimal divide method, with a scale set, the result is correct:

Decimal num = 2.10;
Decimal denom = 2.0;
Decimal result = num / denom;
system.debug('\n\nresult='+result);
Decimal resultdiv =num.divide(denom,2);
system.debug('\n\ndivtest'+resultdiv);
System.debug('Result2=' + result.setScale(2).toPlainString());
system.debug('\n\n');
if (result != 1.05){
System.debug('Not equal!');
}

if (resultdiv != 1.05){
System.debug('Divide Method Not equal!');
}

Additionally, the documention indicates the "/" symbol is used on an integer or a double.  If you do your calculation with a double, the answer also comes out correct.

/ Division operator. Divides x, an Integer or Double, by y, another Integer or Double. Note that if a double is used, the result is a Double.

Message Edited by JimRae on 06-26-2009 04:51 PM
This was selected as the best answer
james2000
Thanks, the divide method does indeed work. So does using a Double. I'm curious why the / operator is allowed for Decimal if it can potentially return faulty or unexpected results though.
Message Edited by james2000 on 06-26-2009 05:22 PM