Overview

Namespaces

  • Apptus
    • ESales
      • Connector
        • Report
        • Time
    • Util
      • Cache
  • PHP
  • Overview
  • Namespace
  • Class
  • Tree
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 
<?php
namespace Apptus\ESales\Connector\Report;

/**
 * A wrapper class for a double representing a rate.
 *
 * The class has methods to format the rate as percentage or per mille.
 */
class Rate {
    private $v;

    /**
     * @param double
     *            The value.
     */
    public function __construct($v) {
        $this->v = $v;
    }

    public static function NaN() {
        return new Rate(NAN);
    }

    public function __toString() {
        return $this->percent(2);
    }

    /**
     * Formats the rate as percentage with $decimals number of decimals.
     *
     * @param int
     *            The number of decimals to use when the rate is formatted.
     * @return string
     *            The rate formatted as percentage. Example: 15.4 %
     */
    public function percent($decimals) {
        if (is_nan($this->v)) {
            return (string) $this->v;
        }
        return number_format($this->v * 100, $decimals) . ' %';
    }

    /**
     * Formats the rate as per mille with {@code decimals} number of decimals.
     *
     * @param int
     *            The number of decimals to use when the rate is formatted.
     * @return string
     *            The rate formatted as per mille. Example: 7.25 ‰
     */
    public function perMille($decimals) {
        if (is_nan($this->v)) {
            return (string) $this->v;
        }
        return number_format($this->v * 1000, $decimals) . ' ‰';
    }

    /**
     * @return float
     *            The rate in decimal form.
     */
    public function value() {
        return $this->v;
    }
}
Apptus ESales Connector PHP API documentation generated by ApiGen