Dim Me as xlNoob
Board Regular
- Joined
- Nov 12, 2017
- Messages
- 107
Hi all,
I have written a function which is able to convert decimal fractions up to 999,999,999.999999 into a binary fraction. It seems to work pretty well but I have an issue with the output I'm getting being limited to 54 characters in total. Have I limited myself with the way I've handled the variables or something? Also it doesn't accept integers over 999,999,999 is this a limitation that I've somehow coded into the function?
It seems to handle smaller numbers with longer fraction parts like 10.999999999999 but still limited to 54 characters. But when I get to 999,999,999.999999 it won't let me add any more decimal places, excel just automatically cuts anything off after 6 decimal places.
Overall, the precision is pretty good but I just want to know if I'm somehow limiting the function or if it is excel.
Additionally, if anyone has a good idea on how to identify when the fractional part of the number starts repeating (remembering it could be repeating 1 or 2 digits, or it could be repeating 5, or 10 digits. I didn't really have any ideas on how I could get the code to identify this and exit the loop when it happens.
All comments/feedback welcome!
I have written a function which is able to convert decimal fractions up to 999,999,999.999999 into a binary fraction. It seems to work pretty well but I have an issue with the output I'm getting being limited to 54 characters in total. Have I limited myself with the way I've handled the variables or something? Also it doesn't accept integers over 999,999,999 is this a limitation that I've somehow coded into the function?
Code:
Function DECTOBIN(DecIn As Double, Optional FractionLen As Long = 20) As String
Dim intDecIn As Long, fDecIn As Double, Bin As Long, n As Long
DECTOBIN = ""
intDecIn = Int(DecIn)
fDecIn = DecIn - intDecIn
For Bin = 0 To intDecIn
If 2 ^ Bin - 1 >= DecIn Then Exit For
Next Bin
For n = Bin To 1 Step -1
If intDecIn < 2 ^ (n - 1) Then DECTOBIN = DECTOBIN & "0"
If intDecIn >= 2 ^ (n - 1) Then
DECTOBIN = DECTOBIN & "1"
intDecIn = intDecIn - 2 ^ (n - 1)
End If
Next n
If DECTOBIN = "" Then DECTOBIN = "0"
If fDecIn > 0 Then
DECTOBIN = DECTOBIN & "."
Do
fDecIn = fDecIn * 2
DECTOBIN = DECTOBIN & Int(fDecIn)
fDecIn = fDecIn - Int(fDecIn)
counter = counter + 1
Loop Until fDecIn = 0 Or counter = FractionLen
End If
End Function
It seems to handle smaller numbers with longer fraction parts like 10.999999999999 but still limited to 54 characters. But when I get to 999,999,999.999999 it won't let me add any more decimal places, excel just automatically cuts anything off after 6 decimal places.
Overall, the precision is pretty good but I just want to know if I'm somehow limiting the function or if it is excel.
Additionally, if anyone has a good idea on how to identify when the fractional part of the number starts repeating (remembering it could be repeating 1 or 2 digits, or it could be repeating 5, or 10 digits. I didn't really have any ideas on how I could get the code to identify this and exit the loop when it happens.
All comments/feedback welcome!