/
Standard.php
62 lines (51 loc) · 1.53 KB
/
Standard.php
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
<?php
/**
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2015-2024
* @package Base
* @subpackage View
*/
namespace Aimeos\Base\View\Helper\Number;
/**
* View helper class for formatting numbers.
*
* @package Base
* @subpackage View
*/
class Standard
extends \Aimeos\Base\View\Helper\Base
implements \Aimeos\Base\View\Helper\Number\Iface
{
private string $dsep;
private string $tsep;
private int $decimals;
/**
* Initializes the Number view helper.
*
* @param \Aimeos\Base\View\Iface $view View instance with registered view helpers
* @param string $decimalSeparator Character for the decimal point
* @param string $thousandsSeperator Character separating groups of thousands
* @param int $decimals Number of decimal digits
*/
public function __construct( \Aimeos\Base\View\Iface $view, string $decimalSeparator = '.', string $thousandsSeperator = '', int $decimals = 2 )
{
parent::__construct( $view );
$this->dsep = $decimalSeparator;
$this->tsep = $thousandsSeperator;
$this->decimals = $decimals;
}
/**
* Returns the formatted number.
*
* @param int|double|string $number Number to format
* @param int|null $decimals Number of decimals behind the decimal point or null for default value
* @return string Formatted number
*/
public function transform( $number, int $decimals = null ) : string
{
if( $decimals === null ) {
$decimals = $this->decimals;
}
return number_format( (double) $number, $decimals, $this->dsep, $this->tsep );
}
}