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

Fractions of a cent #40

Closed
CMCDragonkai opened this issue Jan 9, 2014 · 8 comments
Closed

Fractions of a cent #40

CMCDragonkai opened this issue Jan 9, 2014 · 8 comments
Milestone

Comments

@CMCDragonkai
Copy link

Does this work for fractions of a cent? How accurate is it if I use the multiply and pass in something like 0.1234?

@camspiers
Copy link
Contributor

The core of multiply uses:

(int) round($this->amount * $multiplier, 0, $rounding_mode)

It works as follows:

  1. Multiply amount by specified integer or float multiplier
  2. Round to 0 dp using PHP round and the specified rounding mode
  3. Type cast to an integer
  4. Create new Money value object

So the answer is yes, it can do what you are asking, in the sense that you can have a float multiplier, but remember that the Money class works in the smallest currency unit, so in the case of USD as an example, you can't get sub-cents values. 1 cent is the smallest unit, and the multiply method will respect that and your rounding mode to arrive at the new value for your new Money value object.

@mathiasverraes
Copy link
Collaborator

If you need high precision, you could build a BigMoney class. See
http://www.joda.org/joda-money/ for inspiration. It's similar to Money but
never loses the precision. It should probably use the bcmath php extension.
On 9 Jan 2014 21:44, "Cam Spiers" [email protected] wrote:

The core of multiply uses:

(int) round($this->amount * $multiplier, 0, $rounding_mode)

It works as follows:

  1. Multiply amount by specified integer or float multiplier
  2. Round to 0 dp using PHP round and the specified rounding mode
  3. Type cast to an integer
  4. Create new Money value object

So the answer is yes, it can do what you are asking, in the sense that you
can have a float multiplier, but remember that the Money class works in the
smallest currency unit, so in the case of USD as an example, you can't get
sub-cents values. 1 cent is the smallest unit, and the multiply method will
respect that and your rounding mode to arrive at the new value for your new
Money value object.


Reply to this email directly or view it on GitHubhttps://github.com//issues/40#issuecomment-31974083
.

@CMCDragonkai
Copy link
Author

Thanks @mathiasverraes based on googling for joda-money I found this class: https://github.com/ikr/money-math-php

@mathiasverraes mathiasverraes added this to the 3.1.0 release milestone Mar 22, 2014
@keiosweb
Copy link

keiosweb commented Feb 8, 2015

announcing Keios/MoneyRight - bcmath based money object heavily based on your work @mathiasverraes, we were using your library, so MoneyRight has API matching 1.2.1 release and can work as drop-in replacement for projects depending on GAAP 4 decimal points precision.

@niepi
Copy link

niepi commented Jun 25, 2015

This is also a problem when you use allocate with floats and you have results that have fractions of cents. Those then are cut off and will lead to errors. I don't think this is technically a bug, but I was not aware of this.

            $discountValue = $discountValue / 100;
            $remainingValue = 100 - $discountValue;
            list($discount, $remaining) = $money->allocate([$discountValue, $remainingValue]);
            $money = $money->subtract($discount);

$69 will be $63,82 instead of $63,825

@frederikbosch
Copy link
Member

Let's make this happen too. Why not create a new class PreciseMoney as indicated in #111 by @mathiasverraes?

I think we can reuse the calculators are created in PR #115, but rounding should be extended with precision. Only bcmath can help us out in this case, because GMP is still waiting for RFC GMP Floating Point Numbers to be accepted yet.

@sagikazarmark Do you have any specific ideas on this topic?

@fedeisas
Copy link

The rounding should be performed upon instantiation or formatting of the Money instances?

@frederikbosch
Copy link
Member

This is a duplicate of #7.

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

8 participants