ext-decimal icon indicating copy to clipboard operation
ext-decimal copied to clipboard

How to display the value with comma as decimal separator and dot as thousand separator?

Open cawecoy opened this issue 5 years ago • 3 comments

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.

cawecoy avatar Jul 01 '20 17:07 cawecoy

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 = ',') 

rtheunissen avatar Jul 01 '20 23:07 rtheunissen

I'll work on adding that to 1.x and 2.x 👍

rtheunissen avatar Jul 01 '20 23:07 rtheunissen

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

CViniciusSDias avatar Oct 23 '20 13:10 CViniciusSDias