How does Excel treat numerics differently

alexskinner

New Member
Joined
May 24, 2016
Messages
9
I am testing one value as a multiple of another and suspect that they are stored differently.
I have a routine accumulating money values in various denominations.
There is a text box for each coin denomination ( eg 50 cent,20 cent, 10 cent etc) called tb10cents, tb20cents etc.
I wish to prevent users from entering 65 cents in the 20cent textbox (for example) .
My routine is:
Iftb20Cents.Value/0.2 <> Round(tb20cents.Value/2) then
Display an error message and return theuser to the input area
Else
Perform the required actions
Endif
This works well for some values but not for all. For example is the text box contains 0.60 (which is a multiple of 20 cents) the error message is displayed.
I suspect that it s because Excel treats numbers differentlybut don’t know how to overcome the problem.
I have tried If tb20Cents.Value Mod 2 <> 0 but thatproduces the same result.
Can anyone provide a solution and if possible the reasoningbehind the problem.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You are possibly hitting a floating point precision issue. Please see the link below for explanation and some methods to alleviate the issue...

https://support.microsoft.com/en-us...ithmetic-may-give-inaccurate-results-in-excel

Thank Mark,

This was as I expected. I thought the rounding and/or Mod would overcome the problem but I have discovered a solution.

I have forced the value in the textbox to 2 decimal places and tested against that.
My code now reads If Round(tb20cents.Value/0.2 ,2) <> Round(tb20Cents.Value/0.2 , 0) then etc
It works

Alex
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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