aleostudio/currency-formatter

A simple PHP package to format EUR-USD and other currencies and currency<->float conversion

dev-master 2019-07-19 10:30 UTC

This package is auto-updated.

Last update: 2024-05-12 19:59:12 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