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



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
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

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).
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,105,975
Messages
5,508,517
Members
408,688
Latest member
Bhojraj

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top