Division of not so little numbers
Haskell, 87 bytes
(a#b)c|s<-show$div(a*10^c)b,l<-length s-c,(h,t)<-splitAt l s=['0'|l<1]++h++['.'|c>0]++t
Usage example: (13#7)27
-> "1.857142857142857142857142857"
.
23 bytes to handle the c==0
case and using a leading zero instead of things like .5
.
How it works: multiply a
with 10^c
, divide by b
, turn into a string, split where the .
must be inserted, join both parts with a .
in-between and fix the edge cases.
05AB1E, 17 13 11 19 14 bytes
Input in the form b, a, c
.
Saved 5 bytes thanks to Grimy.
‰`¹+I°*¹÷¦'.sJ
Try it online!
Perl 6, 58 57 55 48 bytes
{(($^a.FatRat/$^b*10**$^c).Int.FatRat/10**$c).base(10,$c)}
{(Int($^a.FatRat/$^b*10**$^c).FatRat/10**$c).base(10,$c)}
{base Int($^a.FatRat/$^b*10**$^c).FatRat/10**$c: 10,$c}
{base ($^a*10**$^c div$^b).FatRat/10**$c: 10,$c}
What is fairly annoying is that it could be shortened to just {($^a.FatRat/$^b).base(10,$^c)}
if it was allowed to round to the nearest value.
Explanation:
# bare block lambda with 3 placeholder parameters 「$^a」, 「$^b」 and 「$^c」
{
(
(
# create an Int containing all of the digits we care about
$^a * 10 ** $^c div $^b
).FatRat / 10**$c # turn it into a Rational
).base( 10, $c ) # force it to display 「$c」 digits after the decimal point
}