mgcodeur/laravel-currency-converter

Laravel货币转换器:轻松在Laravel应用程序中转换货币,无需API密钥。它快速、简单且完全免费。

1.0.5 2024-04-29 09:06 UTC

This package is auto-updated.

Last update: 2024-08-31 09:21:53 UTC


README

Latest Version on Packagist

GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel货币转换器:轻松在Laravel应用程序中转换货币,无需API密钥。它快速、简单且完全免费。

📦 安装

您可以通过composer安装此包

composer require mgcodeur/laravel-currency-converter

Composer安装了Laravel货币转换器包后,您可以运行currency-converter:install Artisan命令

php artisan currency-converter:install

✍🏻 基本用法

转换一种货币到另一种货币

// convert 10 USD to MGA
$convertedAmount = CurrencyConverter::convert(10)
            ->from('USD')
            ->to('MGA')
            ->get();

dd($convertedAmount);

注意:不要忘记导入CurrencyConverter Facades

use Mgcodeur\CurrencyConverter\Facades\CurrencyConverter;

转换所有货币

当您没有指定to方法时,您可以转换所有货币。

// convert 5 EUR to all currencies
$convertedAmount = CurrencyConverter::convert(5)
            ->from('EUR')
            ->get();

dd($convertedAmount);

获取所有货币

要获取所有货币,您可以使用currencies方法。

$currencies = CurrencyConverter::currencies()->get();

dd($currencies);

格式化输出结果

您可以使用format方法来格式化输出结果,而不是使用get方法。

// convert 10 USD to EUR and format the result
$convertedAmount = CurrencyConverter::convert(10)
            ->from('USD')
            ->to('EUR') // you don't need to specify the to method if you want to convert all currencies
            ->format();

dd($convertedAmount);

默认情况下,千位分隔符是逗号(,),小数分隔符是点(.)。您可以在发布的配置文件中更改这些分隔符(config/currency-converter.php)。
如果您打开配置文件,您将看到以下代码

return [
    'currency' => [
        'format' => [
            'decimals' => 2, // change this to 0 if you want result like 1.000, or 3 if you want result like 1.000.000
            'decimal_separator' => ',', // change this to '.' if you want result like 1.000,00, or space if you want result like 1 000,00
            'thousand_separator' => '.', // change this to ',' if you want result like 1,000.00, or '.' if you want result like 1 000.00
        ]
    ],
];

🪴 项目活动

Alt