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
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi Michael,

Try this: =ABS(LEFT(LOG(AK351-BU351),FIND(".",LOG(AK351-BU351))-1))

It works by taking firstly taking the difference between the two numbers and then calculating the Log10 of that number, before simply removing the initial digit which gives the 'scale' of the number (I think 'scale' is the wrong name, but you get the idea).

HTH
 
Upvote 0
Ya know, I really didn't think this would be possible via a formula, but Peter has proven me wrong. His formula worked great for me (although I added "-1" to the end of it to get a more accurate count).

However, before he posted, and whilst I had it in my thick head that a formula approach wouldn't work, I did it in VBA. So, at least you have a couple of options.

Code:
Sub findDecimalPlace()
Dim i As Long
Dim j
Dim firstNum As Double
Dim secondNum As Double
firstNum = Range("AK351").Value
secondNum = Range("BU351").Value
j = 10
    
For i = 1 To Len(Range("AK351").Value)
    If Int(firstNum * j) <> Int(secondNum * j) Then
        MsgBox "Numbers match to " & i - 1 & " decimal places."
        Exit Sub
    End If
    j = j * 10
Next i
MsgBox "Numbers match exactly."
End Sub

Regards,

CJ
 
Last edited:
Upvote 0
It returns #NUM!

Peter's formula returns #NUM! if AK351 is a smaller number than BU351 or if the two are equal.

I was also incorrect by adding "-1" to the end of his formula -- it counts correctly as-is provided AK351 >
BU351.

Regards,

CJ
 
Last edited:
Upvote 0
Hi Michael,

Try this: =ABS(LEFT(LOG(AK351-BU351),FIND(".",LOG(AK351-BU351))-1))

It works by taking firstly taking the difference between the two numbers and then calculating the Log10 of that number, before simply removing the initial digit which gives the 'scale' of the number (I think 'scale' is the wrong name, but you get the idea).
I think you were on the right track using the LOG function of the difference. I believe this will do what the OP wants...

=INT(ABS(LOG(ABS(AK351-BU351))))
 
Upvote 0
Hi Michael,

Your answer surprised me, but then I managed to get my formula to do exactly the same! It appears dependent on whether you substract one number from the other or vice versa!! I've failed to identify the reason. Consequently I've tweaked the formula as shown below. This now seems to work consistently for me.

=IFERROR(ABS(LEFT(LOG(AK351-BU351),FIND(".",LOG(AK351-BU351))-1)), ABS(LEFT(LOG(BU351-AK351),FIND(".",LOG(BU351-AK351))-1)))

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.
 
Upvote 0
I think you were on the right track using the LOG function of the difference. I believe this will do what the OP wants...

=INT(ABS(LOG(ABS(AK351-BU351))))
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)))
 
Upvote 0
Peter's formula also returned #NUM! for me for that sample data. One issue, but I think not the only one, is that LOG() requires a positive argument.

In any case, I think that with so many decimal places, particularly where the difference lies quite a long way along those decimal places, any formula that does arithmetic calculating with those numbers is likely to run into Excel's "Floating-point arithmetic may give inaccurate results" problem.

I have made the assumption that any whole number part of the pair will be the same & offer this rather lengthy formula as a possibility - only lightly tested.

Excel Workbook
AKBUBV
351347.980813837264347.9808138598817
3521.241.231
3532.32.31
354347.980813837264347.98081383726412
35523569.0005002323569.000400233
35656.356.60
Compare decimals



Edit: Forgot to emphasise that this is an array formula so should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}. The formula can then be copied down.
 
Last edited:
Upvote 0
Rick,

As ever a much tidier solution than mine. I did think when I was investigating the #NUM issue that it seemed overly complicated - should have followed that train of thought!

And now I've engaged brain I realise that the #NUM issue is due to not being able to calculate the Log of a negative number!!

I suspect the OP will be very happy with your solution; and I always learn something when I come here!

Regards
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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