<?php
 
 
/*
 
 * Number
 
 * @desc
 
 * @example
 
 
echo "\n######################\n";
 
$num = new Number(10.19212);
 
$num->round();
 
echo "ROUND            ".$num;
 
 
$num = new Number(10.19212);
 
$num->ceil();
 
echo "\nCeil             ".$num;
 
 
$num = new Number(10.19212);
 
$num->floor();
 
echo "\nFloor            ".$num;
 
 
$num = new Number(101010.19212);
 
$num->format();
 
echo "\nFormat EN        ".$num;
 
 
$num = new Number(101010.19212);
 
$num->format(2,",",".");
 
echo "\nFormat DE        ".$num;
 
 
$num = new Number(101010.19212);
 
$num->format(2,","," ");
 
echo "\nFormat F         ".$num;
 
 
$num = new Number(101010.19212);
 
$num->prepend("EUR ")->format(2,","," ");
 
echo "\nMoney EUR        ".$num;
 
 
$num = new Number(101010.19212);
 
$num->append(" EUR")->format(2,",",".");
 
echo "\nMoney EUR        ".$num;
 
 
$num = new Number(10.19212);
 
$num->format(2,",",".")->padLeft(10);
 
echo "\nPadLeft          ".$num;
 
 
$num = new Number(10.19212);
 
$num->format(2,",",".")->padRight(10);
 
echo "\nPadRight         ".$num;
 
 *
 
 * @author Thomas Schaefer
 
 */
 
class Number {
 
 
    private $error;
 
    private $string;
 
    private $pend = 0;
 
    private $number = 0;
 
 
    public function __construct($number=0) {
 
        $this->number = $number;
 
    }
 
    
 
    /**
 
     * casting functions
 
     */
 
    /**
 
     * toInt
 
     * @return void
 
     */
 
    public function toInt(){
 
        $this->number = (int) $this->number;
 
        return $this;
 
    }
 
 
    /**
 
     * toFloat
 
     * @return void
 
     */
 
    public function toFloat(){
 
        $this->number = (float) $this->number;
 
        return $this;
 
    }
 
 
    /**
 
     * formating functions
 
     */
 
    /**
 
     * format
 
     * @param int $decimals
 
     * @param string $dec_point
 
     * @param string $thousands_sep
 
     * @return void
 
     */
 
    public function format($decimals=2, $dec_point=".", $thousands_sep=""){
 
        $this->number = number_format($this->number, $decimals, $dec_point, $thousands_sep);
 
        return $this;
 
    }
 
 
    /**
 
     * Round
 
     * @param int $decimals
 
     * @return void
 
     */
 
    public function round($decimals=0){
 
        $this->number = round($this->number, $decimals);
 
        return $this;
 
    }
 
 
    /**
 
     * Ceil
 
     * @return void
 
     */
 
    public function ceil(){
 
        $this->number = ceil($this->number);
 
        return $this;
 
    }
 
 
    /**
 
     * Floor
 
     * @return void
 
     */
 
    public function floor(){
 
        $this->number = floor($this->number);
 
        return $this;
 
    }
 
 
    /**
 
     * toHex
 
     * @return string
 
     */
 
    public function toHex(){
 
        return dechex($this->number);
 
    }
 
 
    /**
 
     * fromHex
 
     * @param string $hex_string
 
     * @return void
 
     */
 
    public function fromHex($hex_string){
 
        $this->number = hexdec($hex_string);
 
        return $this;
 
    }
 
 
    /**
 
     * toBin
 
     * @return string
 
     */
 
    public function toBin(){
 
        return decbin($this->number);
 
    }
 
 
    /**
 
     * fromBin
 
     * @param string $binary_string
 
     * @return void
 
     */
 
    public function fromBin($binary_string){
 
        $this->number = bindec($binary_string);
 
        return $this;
 
    }
 
 
    /**
 
     * padLeft
 
     * @param int $length
 
     * @return void
 
     */
 
    public function padLeft($length){
 
        $this->number = str_pad($this->number,$length,"0",STR_PAD_LEFT);
 
        return $this;
 
    }
 
 
    /**
 
     * padRight
 
     * @param int $length
 
     * @return void
 
     */
 
    public function padRight($length){
 
        $this->number = str_pad($this->number,$length,"0",STR_PAD_RIGHT);
 
        return $this;
 
    }
 
 
    /**
 
     * append
 
     * @param string $string
 
     * @return void
 
     */
 
    public function append($string="€"){
 
        $this->string = $string;
 
        $this->pend = 1;
 
        return $this;
 
    }
 
 
    /**
 
     * prepend
 
     * @param string $string
 
     * @return prepend
 
     */
 
    public function prepend($string="€"){
 
        $this->string = $string;
 
        $this->pend = 2;
 
        return $this;
 
    }
 
 
    /**
 
     *
 
     * @return float|int
 
     */
 
    public function get() {
 
        return $this->number;
 
    }
 
 
    /**
 
     * __toString
 
     * @return int
 
     */
 
    public function __toString(){
 
        if($this->error instanceof Exception){
 
            return $this->error;
 
        }
 
        switch($this->pend){
 
            case 1:
 
                return (string) $this->number .$this->string;
 
            case 2:
 
                return (string) $this->string . $this->number;
 
        }
 
        return (string) $this->number;
 
    }
 
}
 
 
 |