Real base conversion
JavaScript (ES8), 81 74 71 bytes
f=
(x,b,n,g=x=>x.toString(b))=>g(x-x%1)+'.'+g(x%1).substr(2,n).padEnd(n,0)
<div oninput=o.textContent=f(+x.value,b.value,n.value)><input id=x><input type=number min=2 max=36 value=10 id=b><input type=number min=1 max=20 value=10 id=n><pre id=o>
Works for x
between 1e-6
and 1e21
, b
from 2
to 36
(exactly as required) and n
from 1
to anything from 10
to 48
depending on the base before floating-point errors creep in. Edit: Saved 7 bytes with help from @Birjolaxew. Saved a further 3 bytes with help from @tsh. Previous 74-byte version also worked with negative numbers:
f=
(x,b,n,[i,d]=`${x.toString(b)}.`.split`.`)=>i+`.`+d.slice(0,n).padEnd(n,0)
<div oninput=o.textContent=f(+x.value,b.value,n.value)><input id=x><input type=number min=2 max=36 value=10 id=b><input type=number min=1 max=20 value=10 id=n><pre id=o>
Python 2, 153 149 144 137 135 109 bytes
def f(x,b,m):
i=int(x);s=[];t=[]
while i:s=[i%b]+s;i/=b
while m:m-=1;x=x%1*b;t+=[int(x)]
return s or[0],t
Hadn't noticed I can just return the digits as numbers, so that makes it a lot simpler. Returns two lists of digits, first for the integer part, second for the fractional.
Try it online!
Perl 6, 25 bytes
->\x,\b,\n{+x .base(b,n)}
Try it
Expanded:
-> \x, \b, \n {
+x # make sure it is a Numeric
.base( b, n ) # do the base conversion
}
Note that the space is so that it is parsed as (+x).base(b,n)
not +( x.base(b,n) )
.