Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cawecoy opened this issue Jul 1, 2020 · 3 comments

Comments

@cawecoy
Copy link

cawecoy commented Jul 1, 2020

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 cawecoy changed the title Using comma as decimal separator and dot as thousand separator Display the value with comma as decimal separator and dot as thousand separator Jul 1, 2020
@cawecoy cawecoy changed the title Display the value with comma as decimal separator and dot as thousand separator How to display the value with comma as decimal separator and dot as thousand separator? Jul 1, 2020
@rtheunissen
Copy link
Contributor

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
Copy link
Contributor

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

@CViniciusSDias
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants