Program to compute division upto n decimal places | PHP program to compute division upto n decimal places . ; Base cases ; Since n <= 0 , don 't   compute after the decimal ; Handling negative numbers ; Integral division ; Now one by print digits after dot using school division method . ; Driver Code
< ? php function precisionCompute ( $ x , $ y , $ n ) { if ( $ y == 0 ) { echo " Infinite " , " STRNEWLINE " ; return ; } if ( $ x == 0 ) { echo 0 , " STRNEWLINE " ; return ; } if ( $ n <= 0 ) { echo $ x / $ y , " STRNEWLINE " ; return ; } if ( ( ( $ x > 0 ) && ( $ y < 0 ) ) || ( ( $ x < 0 ) && ( $ y > 0 ) ) ) { echo " - " $ x = $ x > 0 ? $ x : - $ x ; $ y = $ y > 0 ? $ y : - $ y ; } $ d = $ x / $ y ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { echo $ d ; $ x = $ x - ( $ y * $ d ) ; if ( $ x == 0 ) break ; $ x = $ x * 10 ; $ d = $ x / $ y ; if ( $ i == 0 ) echo " . " ; } } $ x = 22 ; $ y = 7 ; $ n = 15 ; precisionCompute ( $ x , $ y , $ n ) ; ? >