How can I simplify a fraction in PHP?
For instance, converting 40/100 to 2/5.
40/100
2/5
The only way I could think of is to do a prime factor
If you have PHP gmp extension, you can do this.
gmp
$num = 40; $den = 100; $gcd = gmp_intval(gmp_gcd((string)$num, (string)$den)); $new_num = $num / $gcd; $new_den = $den / $gcd;