If you want to divide two integers and print the fractional result then you can do it like when you divide by hand in elementary school. The initial division will give you the integer part of the result, then just repeatedly multiply the remainder part by 10 and divide by the divisor until it's zero or you have reached the intended precision to get the fraction
Take the example 32/5
above
Integer part:
32/5 = 6 → Print out "6."
Fractional part:
Remainder: 2, multiply it by 10 → 2*10 = 20
20/5 = 4 → Print out "4"
Remainder: 0, stop here
More complex divisions can also be done like above, just multiply the remainder by 10 after each step. If the result is infinite then stop when you get enough precision. For example: 25/11 = 2.27272727...
25/11 = 2 R 3 → 2.
3*10/11 = 2 R 8 → 2
8*10/11 = 7 R 3 → 7
3*10/11 = 2 R 8 → 2
8*10/11 = 7 R 3 → 7
and so on...
Of course it can be done even faster by multiplying 10N to get N fractional digits at once
Alternatively use floating-point values and do a floating-point division with FDIV
like others said. After that there are numerous ways to convert the float result into string, but it'll be exceedingly difficult to get a correctly rounded result, so it's better to just use libraries made for that purpose.
However for a very simple demonstration then again the above method can also be used:
- Split the integer part of the result and print it followed by
.
- Multiply the integer part by 10, the integer part will be the next fractional digit
- Remove the the integer part and repeat the above step until you reach the desired precision
A rough example is like this, disregarding errors due to binary floating-point properties
11.157
Int part: 11 → print 11.
Fractional part:
0.157*10 = 1.57 → print 1
0.57*10 = 5.7 → print 5
0.7*10 = 7 → print 7
More detailed information can be found in Turn float into string