HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Globalization/NumberFormatInfo.php
<?php


namespace App\Komma\Globalization;

/**
 * Class NumberFormatInfo
 *
 * Inspired on the NumberFormatInfo from c#
 *
 * @see https://docs.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo?view=netframework-4.7.2
 *
 * @package App\Komma\Globalization
 */
class NumberFormatInfo
{
    /** @var int $currencyDecimalDigits */
    private $currencyDecimalDigits = 2;

    /** @var string $currencyDecimalSeparator */
    private $currencyDecimalSeparator = '.';

    /** @var string $currencyGroupSeparator */
    private $currencyGroupSeparator = ',';

    /** @var int $currencyGroupSizes */
    private $currencyGroupSizes = 3;

    /**
     * Converts the given cents to for example dollars. Rounds up decimals to a certain amount of decimal places.
     *
     * @param int $cents
     * @param bool $formatted if true it wil return the value as a string for display. Example an amount of cents would be formatted as 100,000.00.
     * Else the same number would be returned as a php float number that uses a dot as decimal seperator and no group seperator
     * @param bool $alsoZeroPadWhenNoDecimals
     * @return string|float
     */
    public function centsToCurrency(int $cents, $formatted = true, $alsoZeroPadWhenNoDecimals = false)
    {
        if($formatted) {
            $complete = $cents / 100;           //Example 125
            $whole = floor($complete);          //Example 1
            $fraction = round($complete - $whole, $this->currencyDecimalDigits, PHP_ROUND_HALF_UP); //Example 0.25. Rounds a number like 0.2499999999 up to 0.25

            $wholeFormatted = strrev(implode($this->currencyGroupSeparator, str_split(strrev($whole),
                $this->currencyGroupSizes))); //Inserts the currencyGroupSeparator every currencyGroupSizes in the "whole part" of the number.

            $fractionFormatted = substr((string)$fraction, 2,
                strlen((string)$fraction)); //Removes the 0. from for example 0.25

            //Add trailing zero when there is a fraction or when alsoZeroPadWhenNoDecimals is true, and it is less then the currencyDecimalDigits.
            if (($fractionFormatted || $alsoZeroPadWhenNoDecimals) && strlen($fractionFormatted) < $this->currencyDecimalDigits) {
                $padAmount = $this->currencyDecimalDigits - strlen($fractionFormatted);
                $fractionFormatted = $fractionFormatted . str_repeat('0', $padAmount);
            }

            $numberParts = [$wholeFormatted];
            if ($fractionFormatted) {
                $numberParts[] = $fractionFormatted;
            }

            $numberAsStringToReturn = implode($this->currencyDecimalSeparator, $numberParts);

            return $numberAsStringToReturn;
        }
        else {
            return  (float) number_format( round($cents / 100, $this->currencyDecimalDigits, PHP_ROUND_HALF_UP), 2);
        }
    }

    /**
     * @return int
     */
    public function getCurrencyDecimalDigits(): int
    {
        return $this->currencyDecimalDigits;
    }

    /**
     * @param int $currencyDecimalDigits
     */
    public function setCurrencyDecimalDigits(int $currencyDecimalDigits): void
    {
        $this->currencyDecimalDigits = $currencyDecimalDigits;
    }

    /**
     * @return string
     */
    public function getCurrencyDecimalSeparator(): string
    {
        return $this->currencyDecimalSeparator;
    }

    /**
     * @param string $currencyDecimalSeparator
     */
    public function setCurrencyDecimalSeparator(string $currencyDecimalSeparator): void
    {
        $this->currencyDecimalSeparator = $currencyDecimalSeparator;
    }

    /**
     * @return string
     */
    public function getCurrencyGroupSeparator(): string
    {
        return $this->currencyGroupSeparator;
    }

    /**
     * @param string $currencyGroupSeparator
     */
    public function setCurrencyGroupSeparator(string $currencyGroupSeparator): void
    {
        $this->currencyGroupSeparator = $currencyGroupSeparator;
    }

    /**
     * @return int
     */
    public function getCurrencyGroupSizes(): int
    {
        return $this->currencyGroupSizes;
    }

    /**
     * @param int $currencyGroupSizes
     */
    public function setCurrencyGroupSizes(int $currencyGroupSizes): void
    {
        $this->currencyGroupSizes = $currencyGroupSizes;
    }
}