Created
April 12, 2018 01:49
-
-
Save jayrm/efbfc5acc7a55bf0c95de3c2e045bfb7 to your computer and use it in GitHub Desktop.
Quick and Dirty Double to Decimal Expansion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'' quick and dirty double to decimal expansion | |
'' inpsired by | |
'' http://www.exploringbinary.com/quick-and-dirty-floating-point-to-decimal-conversion/ | |
function QD_Dbl2Dec( byval fp as double ) as string | |
dim as string d, s = "+" | |
if( fp < 0 ) then s = "-": fp = -fp | |
if( fp < 1 ) then s &= "0" | |
dim as double i = fix(fp), f = frac(fp) | |
while( i > 0 ) | |
d = chr(48 + (i mod 10)) & d | |
i \= 10 | |
wend | |
d = s & d & "." | |
while( f > 0 ) | |
f *= 10: i = fix(f): f = frac(f) | |
d &= chr( 48 + i ) | |
wend | |
function = d | |
end function | |
print QD_Dbl2Dec( 3.141596 ) | |
print QD_Dbl2Dec( -1.0125 ) | |
print QD_Dbl2Dec( 1.123e-27 ) | |
/' OUTPUT: | |
+3.14159599999999983310772222466766834259033203125 | |
-1.0124999999999999555910790149937383830547332763671875 | |
+0.000000000000000000000000001122999999999999776179038235568441450595855712890625 | |
'/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment