blackjew/number-to-words

Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.

v1.2.1 2021-04-29 15:21 UTC

This package is auto-updated.

Last update: 2024-12-20 17:25:22 UTC


README

Travis Code Climate Test Coverage Latest Stable Version

This library allows you to convert a number to words.

Installation

Add package to your composer.json by running:

$ composer require blackjew/number-to-words

Usage

This library currently has two types of number-to-words transformations: number and currency. In order to use a specific transformer for certain language you need to create an instance of NumberToWords class and then call a method which creates a new instance of a transformer;

Number Transformer

Before using a transformer, it must be created:

use BlackJew\NumberToWords\NumberToWords;

// create the number to words "manager" class
$numberToWords = new NumberToWords();

// build a new number transformer using the RFC 3066 language identifier
$numberTransformer = $numberToWords->getNumberTransformer('en');

Then it can be used passing in numeric values to the toWords() method:

$numberTransformer->toWords(5120); // outputs "five thousand one hundred twenty"

Currency Transformer

Creating a currency transformer works just like a number transformer.

use BlackJew\NumberToWords\NumberToWords;

// create the number to words "manager" class
$numberToWords = new NumberToWords();

// build a new currency transformer using the RFC 3066 language identifier
$currencyTransformer = $numberToWords->getCurrencyTransformer('en');

Then it can be used passing in numeric values for amount and ISO 4217 currency identifier to the toWords() method:

$currencyTransformer->toWords(25000, 'UGX'); // outputs "twenty-five thousand ugandan shillings"

Bare in mind, the currency transformer accepts integers as the amount to transform. It means that if you store amounts as floats (e.g. 4.99) you need to multiply them by 100 and pass the integer (499) as an argument.

Available locale