Skip to content

Commit

Permalink
Add some method in number filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Dec 13, 2023
1 parent ca1430b commit 6549aec
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Filter/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,64 @@ public static function format(
);
}

/**
* Number format for money
* @param mixed $variable
* @param mixed $decimal
* @param string $decimalPoint
* @param string $separator
* @return float|mixed
*/
public static function formatMoney(
$variable,
$decimal = 0,
$decimalPoint = '.',
$separator = ','
) {
if (!is_numeric($variable)) {
return $variable;
}

$number = (string) $variable;
if (strpos($number, '.') === false && strpos($number, ',') === false) {
$decimal = 0;
}

return number_format(
(float) $variable,
(int) $decimal,
$decimalPoint,
$separator
);
}

/**
* Return the given number to string
* @param mixed $variable
* @return string|mixed
*/
public static function numberToString($variable)
{
$value = (string) $variable;
if (stripos($value, 'e') !== false) {
// PHP use scientific notation if decimal has 4 zeros
// after dot. so use number format instead of
list($base, $decimal) = explode('E', $value);

// Some system use "," instead of "."
if (strpos($value, ',') !== false) {
$arr = explode(',', $base);
} else {
$arr = explode('.', $base);
}
$separator = '%.' . (string)(strlen($arr[1]) + (abs($decimal) - 1)) . 'f';

$value = sprintf($separator, $variable);
}

return str_replace(',', '.', $value);
}

/**
* Units format
* @param mixed $variable
Expand Down
24 changes: 24 additions & 0 deletions tests/Filter/NumberFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ public function testNumberFormat(): void
$this->assertEquals('4a000', NumberFilter::format('4a000'));
}

public function testFormatMoney(): void
{
$this->assertEquals('4', NumberFilter::formatMoney('4'));
$this->assertEquals('4,000', NumberFilter::formatMoney('4000'));
$this->assertEquals('4,000', NumberFilter::formatMoney('4000', 4));
$this->assertEquals('4,000', NumberFilter::formatMoney('4000', 4, '-'));
$this->assertEquals('4 000', NumberFilter::formatMoney('4000', 4, '-', ' '));
$this->assertEquals('4a000', NumberFilter::formatMoney('4a000'));
}

public function testNumberToString(): void
{
$this->assertEquals('4', NumberFilter::numberToString('4'));
$this->assertEquals('4', NumberFilter::numberToString(4));
$this->assertEquals('4000', NumberFilter::numberToString('4000'));
$this->assertEquals('12.89', NumberFilter::numberToString('12,89'));
$this->assertEquals('4a000', NumberFilter::numberToString('4a000'));
$this->assertEquals('0.00004', NumberFilter::numberToString(4.3E-5));
$this->assertEquals('0.00004', NumberFilter::numberToString('4.3E-5'));
$this->assertEquals('4.00000', NumberFilter::numberToString('4,3E-5'));
$this->assertEquals('0.00004', NumberFilter::numberToString('0,00004'));
$this->assertEquals('45.004', NumberFilter::numberToString('45,004'));
}

public function testSizeFormat(): void
{
$this->assertEquals('4B', NumberFilter::sizeFormat('4'));
Expand Down

0 comments on commit 6549aec

Please sign in to comment.