Ad
Mathematics
Algorithms
Logic
Numbers

Removed duplicate code and multiple executions of fmod by calculating it before all the places where it's needed, reducing probability of copy/paste error and slightly improving performance.
Also added a space before every left bracket, for readability (personal preference).

Code
Diff
  • function calcTokenCost($price, $token) {
        if ($price == 0 && $token == 0) {
            return 0;
        }
        if ($price < $token) {
            return $token;
        }
        $mod = fmod($price, $token);
        if ($mod < ($token / 2)) {
            return $price - $mod;
        }
        return $price + $token - $mod;
    }
    • function calcTokenCost($price, $token){
    • if ($price == 0 && $token == 0){
    • function calcTokenCost($price, $token) {
    • if ($price == 0 && $token == 0) {
    • return 0;
    • }
    • if ($price < $token){
    • if ($price < $token) {
    • return $token;
    • }
    • if (fmod($price, $token) < ($token / 2)){
    • return $price - fmod($price, $token);
    • $mod = fmod($price, $token);
    • if ($mod < ($token / 2)) {
    • return $price - $mod;
    • }
    • return $price + $token - fmod($price, $token);
    • return $price + $token - $mod;
    • }