Torann / laravel-currency

This provides Laravel with currency functions such as currency formatting and conversion using up-to-date exchange rates.
http://lyften.com/projects/laravel-currency
BSD 2-Clause "Simplified" License
392 stars 137 forks source link

No currency conversion function? #49

Closed amitailanciano closed 7 years ago

amitailanciano commented 8 years ago

I was looking for a function to help me convert between currencies, I couldn't find one in the source -- it would be great if there was a function like this built in:

public static function convert($amount, $fromCode, $toCode) {
        $from = app('currency')->getCurrency($fromCode);
        $to = app('currency')->getCurrency($toCode);

        return ($to['value'] / $from['value']) * $amount;
    }

It is not really clear at all that when using the currency function, specifying a currency code different than the config default will actually convert the amount to the specified currency relative to your default currency. I assumed currency(100, 'JPY') would give me ¥100.00, instead I get ¥10,339.00 because my default currency is USD.

Torann commented 7 years ago

It's in there, but it's super messy. I'm finishing up version 1.0 of the package. It makes things a lot more simple. I haven't pushed the documenting for it yet.

If you want to try out version 1.0 here's how you would install it:

composer require torann/currency:1.0.x-dev

And here is the basic use doc:

Converting

This is a shortcut to the most commonly used convert method. Which converts the given amount into the provided currency.

currency($amount, $from = null, $to = null)

Arguments:

$amount - The float amount to convert $from - The current currency code of the amount. If not set the application default will be used (see config/currency.php file). $to - The currency code to convert the amount to. If not set the user set currency is used.

Usage:

echo currency(12.00);               // Will format the amount using the user selected currency
echo currency(12.00, 'USD', 'EUR'); // Will format the amount from the default currency to EUR

Formatting

Quickly parse a given amount into the proper currency format. This is a shortcut to the most commonly used format method.

currency_format($amount, $code = null)

Arguments:

$amount - The float amount to convert $code - The current currency code of the amount. If not set the application default will be used (see config/currency.php file).

Usage:

echo currency_format(12.00);        // Will format the amount using the application default currency
echo currency_format(12.00, 'EUR'); // Will format the amount from the default currency to EUR
Torann commented 7 years ago

Here's the branch: https://github.com/Torann/laravel-currency/blob/1.0

The database has changed some too, take a look: https://github.com/Torann/laravel-currency/blob/1.0/src/migrations/2013_11_26_161501_create_currency_table.php

gaurav112 commented 7 years ago

Was looking way to include 1.0. Should have come here first, would have saved some time. Any chance you can put this in the docs ??

Torann commented 7 years ago

The docs now reflects the changes made in v1.0

http://lyften.com/projects/laravel-currency/doc

You can include v1.0 in you project by requiring the master branch:

"torann/currency": "dev-master"

I would only do this if you are willing to test it out. I have most of the bugs worked out, but I'm still going through it all. Also if you decide to try the dev-master, please remember to change it to a version number once I release it. If you don't and I release another major release down the line, having dev-master will tell your system to install it. This could cause errors.

Torann commented 7 years ago

Version 1.0 was just released