public function to($unit, $decimals = null, $round = true)
    {
        if (!isset($this->value)) {
            throw new InvalidArgumentException('From value not set');
        }

        if (is_array($unit)) {
            return $this->toMany($unit, $decimals, $round);
        }

        $key = strtolower($unit);

        if (empty($this->units[$key]['base'])) {
            throw new OutOfBoundsException('Unit does not exist');
        }

        if (!isset($this->base_unit)) {
            $this->base_unit = $this->units[$key]['base'];
        }

        if ($this->units[$key]['base'] != $this->base_unit) {
            throw new InvalidArgumentException('Cannot convert between units of different types');
        }

        if (is_callable($this->units[$key]['conversion'])) {
            $result = $this->units[$unit]['conversion']($this->value, true);
        } else {
            $result = $this->value / $this->units[$key]['conversion'];
        }

        if (!isset($decimals)) {
            return $result;
        }

        if ($round) {
            return round($result, $decimals);
        }

        $shifter = $decimals ? pow(10, $decimals) : 1;
        return floor($result * $shifter) / $shifter;
    }
public function to($unit, $decimals = null, $round = true)
    {
        if (!isset($this->value)) {
            throw new InvalidArgumentException('From value not set');
        }

        if (is_array($unit)) {
            return $this->toMany($unit, $decimals, $round);
        }

        $key = strtolower($unit);

        if (empty($this->units[$key]['base'])) {
            throw new OutOfBoundsException('Unit does not exist');
        }

        if (!isset($this->base_unit)) {
            $this->base_unit = $this->units[$key]['base'];
        }

        if ($this->units[$key]['base'] != $this->base_unit) {
            throw new InvalidArgumentException('Cannot convert between units of different types');
        }

        if (is_callable($this->units[$key]['conversion'])) {
            $result = $this->units[$unit]['conversion']($this->value, true);
        } else {
            $result = $this->value / $this->units[$key]['conversion'];
        }

        if (!isset($decimals)) {
            return $result;
        }

        if ($round) {
            return round($result, $decimals);
        }

        $shifter = $decimals ? pow(10, $decimals) : 1;
        return floor($result * $shifter) / $shifter;
    }
public function to ( $ unit , $ decimals = null , $ round = true ) { if ( ! isset ( $ this -> value ) ) { throw new InvalidArgumentException ( 'From value not set' ) ; } if ( is_array ( $ unit ) ) { return $ this -> toMany ( $ unit , $ decimals , $ round ) ; } $ key = strtolower ( $ unit ) ; if ( empty ( $ this -> units [ $ key ] [ 'base' ] ) ) { throw new OutOfBoundsException ( 'Unit does not exist' ) ; } if ( ! isset ( $ this -> base_unit ) ) { $ this -> base_unit = $ this -> units [ $ key ] [ 'base' ] ; } if ( $ this -> units [ $ key ] [ 'base' ] != $ this -> base_unit ) { throw new InvalidArgumentException ( 'Cannot convert between units of different types' ) ; } if ( is_callable ( $ this -> units [ $ key ] [ 'conversion' ] ) ) { $ result = $ this -> units [ $ unit ] [ 'conversion' ] ( $ this -> value , true ) ; } else { $ result = $ this -> value / $ this -> units [ $ key ] [ 'conversion' ] ; } if ( ! isset ( $ decimals ) ) { return $ result ; } if ( $ round ) { return round ( $ result , $ decimals ) ; } $ shifter = $ decimals ? pow ( 10 , $ decimals ) : 1 ; return floor ( $ result * $ shifter ) / $ shifter ; }
Convert from value to new unit
@param string|array $unit
@param null|integer $decimals
@param bool $round
@return mixed
@throws OutOfBoundsException
@throws InvalidArgumentException
Convert from value to new unit