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

fix: decimal conversion #28

Merged
merged 7 commits into from
Mar 13, 2024
Merged

fix: decimal conversion #28

merged 7 commits into from
Mar 13, 2024

Conversation

ntheile
Copy link
Contributor

@ntheile ntheile commented Mar 8, 2024

Fix decimal conversion function and adds support for 3 decimal currencies.

@ntheile ntheile marked this pull request as ready for review March 8, 2024 22:13
Copy link
Contributor

@testerez testerez left a comment

Choose a reason for hiding this comment

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

Do we really need moneyphp? Converting to smallest unit is pretty simple without any external library. Here is an example, copied from zaprite-v2 and ported to PHP:

<?php

function get_currency_smallest_unit_factor($currency) {
    // source: https://stripe.com/docs/currencies
    if (in_array($currency, ['CLP','JPY','KRW','UGX','VND'])) return 1;
    if (in_array($currency, ['BHD','JOD','KWD','OMR','TND'])) return 1000;
    if (in_array($currency, ['BTC', 'LBTC'])) return 100_000_000;
    return 100;
}

function to_smallest_unit($amount, $currency) {
    return round($amount * get_currency_smallest_unit_factor($currency));
}

function from_smallest_unit($amount, $currency) {
    return $amount / get_currency_smallest_unit_factor($currency);
}

For info, I realised zaprite-v2 was not handling 3 decimal currencies correctly. Fixed here: https://github.com/ZapriteApp/zaprite-v2/pull/1336

@ntheile ntheile changed the title fix: add money dependency fix: decimal conversion Mar 11, 2024
@john-zaprite john-zaprite merged commit 6d96077 into main Mar 13, 2024
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.

3 participants