When is an integer not an integer?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,531
Office Version
  1. 365
Platform
  1. Windows
This is probably obvious, but not to me.

I am trying to write a little UDF to convert a probability (0-1) into a fraction (ratio). For example, 25% would become "3/1", 40% would become "1.5/1", etc.

I have that working. Now I want to convert any fractions that are not integers in the numerator to another fraction that is. For example. 40% would become "3/2" instead of "1.5/1". To my addled brain, the code below should work. It even looks like it is working, but it is not. What I am doing is multiplying that numerator (1.5) by the integers from 1 to 10. If any of those results are an integer, I return that value with the multiplier (denom) as the denominator.

In the example below, the UDF is passed "0.4". It converts that to "1.5" (frac). On the second pass through the For loop (denom = 2), the numerator (numer) appears to be "3", but it fails the test against Int(numer). In the Immedite window, numer is "3", but int(numer) is "2".

Can someone tell me what is going on?

1653806349009.png
 
Your method seems to work. I would find the rounding method more intuitive but that's just a personal preference.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Your method seems to work. I would find the rounding method more intuitive but that's just a personal preference.
The problem I have with the rounding method is that I have to choose the number of digits to round to. Excel (or VBA) seems to be doing this under the covers, so why not take advantage of it? If VBA thinks it's an integer, that's good enough for me. 🤔🤨😄
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,705
Members
449,048
Latest member
81jamesacct

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