aleostudio / currency-formatter
A simple PHP package to format EUR-USD and other currencies and currency<->float conversion
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^8.3@dev
This package is auto-updated.
Last update: 2025-03-12 21:48:04 UTC
README
A simple singleton instance with fluent methods to format a float price with the right currency syntax, adding to it an optional suffix or prefix and specifying the decimals number.
Convert a integer/float value to a formatted currency:
CurrencyFormatter::set('EUR')->value(1000.46);
- Will return the formatted string: € 1.000,46
CurrencyFormatter::set('EUR')->value(1000.46)->withPrefix('EURO')->decimals(3);
- Will return the formatted string: EURO 1.000,460
CurrencyFormatter::set('EUR')->value(1000.46)->withSuffix('Eur');
- Will return the formatted string: 1.000,46 Eur
CurrencyFormatter::set('USD')->value(1000.46)->withPrefix('$');
- Will return the formatted string: $ 1,000.46
CurrencyFormatter::set('USD')->value(1000.46)->withPrefix('USD')->decimals(3);
- Will return the formatted string: USD 1,000.460
CurrencyFormatter::set('USD')->value(1000.46)->withSuffix('USD');
- Will return the formatted string: 1,000.46 USD
Convert a currency string to a valid float value
CurrencyFormatter::toFloat('EUR 1.000,45');
- Will return the float value: 1000.45
CurrencyFormatter::toFloat('1,000.45 USD');
- Will return the float value: 1000.45
Installation
Clone the package with the command:
git clone git@github.com:aleostudio/currency-formatter.git
Install its dependencies with:
composer install
Then create a new php file and try this code below
require_once __DIR__ . '/vendor/autoload.php'; use AleoStudio\CurrencyFormatter\CurrencyFormatter; echo CurrencyFormatter::set('EUR')->value(1000.46) . '<br />'; echo CurrencyFormatter::set('EUR')->value(1000.46)->withPrefix('EURO')->decimals(3) . '<br />'; echo CurrencyFormatter::set('EUR')->value(1000.46)->withSuffix('Eur') . '<br />'; echo CurrencyFormatter::set('USD')->value(1000.46)->withPrefix('$') . '<br />'; echo CurrencyFormatter::set('USD')->value(1000.46)->withPrefix('USD')->decimals(3) . '<br />'; echo CurrencyFormatter::set('USD')->value(1000.46)->withSuffix('USD') . '<br />'; echo CurrencyFormatter::toFloat('EUR 1.000,45') . '<br />'; echo CurrencyFormatter::toFloat('1,000.45 USD') . '<br />';
Unit testing
Install PHPUnit and then run the test with the command:
phpunit --bootstrap vendor/autoload.php tests/CurrencyFormatterTest.php
This README was generated with: https://stackedit.io/app