Math.Round causes results not to match

Asdf0qwerty

New Member
Joined
Jul 28, 2010
Messages
19
Hi everyone,

I tried to use the Math.Round function inside my UDF Add-In so that it would return a result rounded of to 2 decimal places.

Also, I have this "test table" wherein I could compare the 1.) Results From Manual (left blue column) from 2.) Results From UDF (right blue column).

I used the IF function to test e.g. =IF(CJ14=CM14,"MATCH", "NO MATCH"). I do not understand why there are "NO MATCH" results even though the columns does match. Why did this happen and what should I do to solve the problem?

1. Here's the image of my worksheet

nomatch.png


2. Here's the return statement of my UDF

Code:
'This statement returns result into Worksheet Cell
MyCurrencyFunction = Math.Round(CDec(ComputeThis(varX, varY)), 2)


I hope anyone can help me out soon. TIA
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
How have you generated the values in the first column (ie NOT the UDF column)?

I would have thought a better comparison would be:

=IF(ROUND(CJ14,2)=CM14,"MATCH", "NO MATCH").
 

Asdf0qwerty

New Member
Joined
Jul 28, 2010
Messages
19
@Richard: Thank you for your help Richard. I do apologize for the very late reply. A lot of troubles and deliverables.

Btw, I tried what you told me, and it worked for all records except for 4 records. The results of my UDF were a 1 cent less than the value it is compared to.

Also, I found out that w/o using the Math.Round in my UDF + using your program statement in the cell, worked a 100% correct in all records.

So, is it safe to just return results from my UDF without using Math.Round? Just want to know what you think about this. Cause, well for me since it works then might as well I stick with not using Math.Round.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
You probably don't need to do it in the UDF (unless you specifically want values to 2 decimal places) but any time you have floating numbers you may have trouble doing exact comparisons because of the limited precision offered by Excel (and the way computers store floating numbers).

hence, when doing comparisons to other floating numbers you should use a rounding function (on both).
 

Watch MrExcel Video

Forum statistics

Threads
1,133,158
Messages
5,657,160
Members
418,363
Latest member
Debating_Earth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top