compare two decimal values and return number of decimal places correct

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
I am looking for a formula that can compare two decimal values and return how many decimal values are correct. Example:
Cell AK351 = 347.980813837264
Cell BU351 = 347.980813859881

I need to compare how many decimal places in cell bu351 are correct compared to cell ak351. In this example the formula would return 7.

Thanks for the help.

Mike
 
Actually, if we are sure the integer part will always be equal, I believe the above formula can be simplified to this...

=-TRUNC(LOG(ABS(AK351-BU351)))
Rick, I'm not sure if you are suggesting that either formula returns the correct answers, but yours returns
7 correctly for the sample given, but ..

2 (not 1) for the values 1.24 and 1.23
#NUM! (not 1) for 2.3 and 2.3
4 (not 3) for 23569.00050023 and 23569.00040023
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I have to say in response to MrIfOnly that I thought I'd captured the decimal point by including the -1 in the Find statements so not really sure why it failed - I guess it has something to do with exactly what example you've tested it against. That makes me concerned that it is not a robust solution - any chance you could show the example that led to the need to modify?

All the best.

Try this: AK351 = 2.1234567891235
BU351 = 2.12345678912345

For some reason it adds one to the count in your original formula and both of Rick's formulas. (It should return 12 but returns 13).

Regards,

CJ
 
Upvote 0
Rick, I'm not sure if you are suggesting that either formula returns the correct answers, but yours returns
7 correctly for the sample given, but ..

2 (not 1) for the values 1.24 and 1.23
#NUM! (not 1) for 2.3 and 2.3
4 (not 3) for 23569.00050023 and 23569.00040023

This is definitely strange, because if you change 1.24 to 1.245 all formulas work as advertised!
 
Upvote 0
Ahh, I think I see a problem with the approach we've adopted of taking logs.

The difference in your example is numerically small so for example 2.50 and 2.45 will calculate incorrectly as it returns 1 when there are no matching decimal digits. It does look as if the character by character solution is required.

I thought this would be simple!!
 
Upvote 0
Rick, I'm not sure if you are suggesting that either formula returns the correct answers, but yours returns
7 correctly for the sample given, but ..

2 (not 1) for the values 1.24 and 1.23
#NUM! (not 1) for 2.3 and 2.3
4 (not 3) for 23569.00050023 and 23569.00040023
Hmm, looks like a bad assumption with even worse testing on my part. :(

Okay, then, how about this. Assuming no more than 15 significant digits maximum, create a defined name for X (yes, just that single letter) with this...

={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

and then use this array-entered** formula...

=IF(A9=B9,0,MAX(IF(INT(A9*10^X)=INT(B9*10^X),X)))

**Commit this formula using CTRL+SHIFT+ENTER and not just Enter by itself


EDIT NOTE: I know we could hard-code the array constant into the formula, but I though that made it kind of ugly looking.
 
Last edited:
Upvote 0
Rick,


Your latest passed all of my tests.

I am, however, going back to suggesting using VBA and converted my code into an UDF simply because it takes just two arguments: range 1 and range 2.

Code:
Function findDecPlc(first As Range, second As Range)
Dim i As Long
Dim j
Dim firstNum As Double
Dim secondNum As Double
firstNum = first.Value
secondNum = second.Value
j = 10
    
For i = 1 To Len(first.Value)
    If Int(firstNum * j) <> Int(secondNum * j) Then
        findDecPlc = i - 1
        Exit Function
    End If
    j = j * 10
Next i
findDecPlc = "MATCH!"
End Function

Please let me know if there are improvements to be made...

Regards,

CJ
 
Upvote 0
Thats ingenious - i think its going to be years before I could have dreamt that solution!!
 
Upvote 0
=IF(A9=B9,0, ...
I don't understand that. If the 2 numbers are equal, surely the number of "correct" decimal places is the number of decimal places in the numbers?


I know we could hard-code the array constant into the formula, but I though that made it kind of ugly looking.
Whilst that is true, I suspect the majority of users would elect that over having to define a name, unless perhaps that name would remove something very long from the worksheet formula. Anyway, it certainly is a valid option.
Of course I have an even longer construct to produce an array in my formula but I elected to cater for any number of possible digits as that would also allow for the case if the values were quite long and entered as text.
 
Upvote 0

Forum statistics

Threads
1,216,031
Messages
6,128,424
Members
449,450
Latest member
gunars

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
Back
Top