Skip to content

Conversation

@dwightwatson
Copy link
Contributor

This may be a little specific to my use-cases, but I often find myself formatting whole amounts without cents.

This exposes an optional precision argument (matching the precision argument on plain-old Number::format() as well.

I wasn't sure whether to move $precision ahead of $locale like the other methods in case it was breaking. But as it's brand new it may not be too bad?

$this->assertSame('$0', Number::currency(0, precision: 0));
$this->assertSame('$2', Number::currency(1.50, precision: 0));

$this->assertSame('-$2', Number::currency(-1.75, precision: 0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dwightwatson Do we have a way to control the rounding mode her? As i don't think that silently rounding up to the nearest integer is what is actually wanted when dealing with money precision wise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so it looks like it does support a ROUNDING_MODE and then there are a heap of constants to control the direction. I'm not sure the nicest way to expose this in the helper without having to take in one of the constants. Do you have any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also worth considering that if there's reason enough to support controlling rounding it should probably also be added to Number::format() for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely like this PR, but I should mention that I think as soon as we start rounding currencies, we are entering dangerous territory. To be fair, just using floating point numbers for currency is also living on the edge in my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While looking for inspiration on how to handle rounding I did note that number_to_currency in Rails also supports precision without control over the rounding direction.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

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

Successfully merging this pull request may close these issues.

4 participants