ext-decimal
ext-decimal copied to clipboard
How to display the value with comma as decimal separator and dot as thousand separator?
Hi.
Is there a way to display the value with comma as decimal separator and dot as thousand separator? Something like:
$decimal->toFixed(2, ',', '.');
or
$decimal->format(2, ',', '.'); // similarly to number_format
Thanks.
Unfortunately that is not supported right now, but you raise a good point in that there no equivalent for number_format. Your best option would be to do a string replace on the result of toFixed. I think a generic format method would be good, as suggested. Perhaps:
function format(int $decimalPlaces, string $decimalPoint = '.', string $thousandsSep = ',')
I'll work on adding that to 1.x and 2.x 👍
You can use the number_format function on the Decimal result with no problem. Just cast it to string and you're good to go.
php > $a = new Decimal\Decimal('0.1');
php > $b = new Decimal\Decimal('0.2');
php > $c = new Decimal\Decimal('0.3');
php > echo $a + $b - $c;
0.0
php > echo number_format((string) ($a + $b - $c), 2, ',', '.');
0,00