2017 © Pedro Peláez
 

library currency-info

Provide basic information to display a currency amount

image

vinaikopp/currency-info

Provide basic information to display a currency amount

  • Wednesday, June 1, 2016
  • by vinai
  • Repository
  • 3 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Currency Info

This repository offers an easy way to get data required to display money values in a given currency:, (*1)

  • Fraction Digits (e.g. 2 for currencies with 100 subunits like USD)
  • Rounding (e.g. 0.05 for currencies like CHF)
  • International Symbol (e.g. € for EUR)
  • Native Symbol (e.g. $ for USD)

To look up the information the (ISO 4217) three letter currency code, e.g. DKK or EUR)., (*2)

Background

Sometimes a currency package that provides all things about currency is overkill. I might just want to render a monetary value.
PHP can natively nicely format currency values, but it doesn't know about the default fraction digits for example.
This is what this package can be used for. It does not provide any functionality in regards to conversion or division or such.
All this package does is provide information about a given currency., (*3)

Installation

Install using composer:, (*4)

$ composer require vinaikopp/currency-info "^2.0.0"

Or add it manually to the composer.json file:, (*5)

{
    "require": {
        "vinaikopp/currency-info": "^2.0.0"
    }
}

Usage

For example, to display a money value with the default number of fraction digits, something like this could be used:, (*6)

Example using static methods, (*7)

use VinaiKopp\CurrencyInfo\StaticAccess\CurrencyInfo;

$currency = 'EUR';
$locale = 'de_DE';
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$decimalPlaces = CurrencyInfo::getFractionDigitsForCurrency($currency);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimalPlaces);

echo $formatter->formatCurrency($value, $currency);

If you use an Dependency Injection Container and would like an instance you can inject, you can use \VinaiKopp\CurrencyInfo\CurrencyInfo., (*8)

Example using a currency info instance, (*9)

public function __construct(\VinaiKopp\CurrencyInfo\CurrencyInfo $currencyInfo)
{
    $this->currencyInfo = $currencyInfo;
    $this->currency = 'USD';
    $this->locale = 'en_US'
}

public function format($amount)
{
    $formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
    $decimalPlaces = $this->currencyInfo->getFractionDigitsForCurrency($this->currency);
    $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimalPlaces);

    return $formatter->formatCurrency($amount, $this->currency);
}

What it is not

This package does not offer capabilities to do money calculations or currency conversions. If you need that, I suggest using mathiasverraes/money., (*10)

Continuous Integration

Service Build Status Coverage Score
Travis Build Status
Scrutinizer Build Status Coverage Quality Score

Where does the data come from?

The map is based on the LocalePlanet Currency Map.
Thanks for their epic efforts!!, (*11)

The Versions

01/06 2016

dev-master

9999999-dev

Provide basic information to display a currency amount

  Sources   Download

BSD-3-Clause

The Requires

  • php ~5.6.0|~7.0.0

 

The Development Requires

by Vinai Kopp

01/06 2016

2.0.1

2.0.1.0

Provide basic information to display a currency amount

  Sources   Download

BSD-3-Clause

The Requires

  • php ~5.6.0|~7.0.0

 

The Development Requires

by Vinai Kopp

31/05 2016

2.0.0

2.0.0.0

Provide basic information to display a currency amount

  Sources   Download

BSD-3-Clause

The Requires

  • php ~5.6.0|~7.0.0

 

The Development Requires

by Vinai Kopp

30/05 2016

1.0.0

1.0.0.0

Provide basic information to display a currency amount

  Sources   Download

BSD-3-Clause

The Requires

  • php ~5.6.0|~7.0.0

 

The Development Requires

by Vinai Kopp