Code Monkey home page Code Monkey logo

number-to-words's Introduction

PHP Number to words converter

CircleCI Code Climate Test Coverage Latest Stable Version

Welcome to number-to-words, a PHP utility that seamlessly transforms numeric values into their corresponding words. Effortlessly convert numbers, such as 123, into expressive and readable formats like "one hundred and twenty-three" with just a few lines of code.

Installation

Add package to your composer.json by running:

$ composer require kwn/number-to-words

Usage

There are two types of number-to-words transformation: number and currency. In order to use a relevant transformer for specific language create an instance of NumberToWords class and call a method that creates a new instance of the desired transformer;

Number Transformer

Create a transformer for specific language using the getNumberTransformer('lang') method:

use 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');

Transformer can be used by passing in numeric values to the toWords() method:

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

It can be also used with a static method:

NumberToWords::transformNumber('en', 5120); // outputs "five thousand one hundred twenty"

Currency Transformer

Creating a currency transformer works just like a number transformer.

use 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(5099, 'USD'); // outputs "fifty dollars ninety nine cents"

It can be also used with a static method:

NumberToWords::transformCurrency('en', 5099, 'USD'); // outputs "fifty dollars ninety nine cents"

Note: The Currency Transformer within this library processes integers; ensure your input amounts are in whole numbers by multiplying any float values by 100 before transformation. For instance, transform 45.67 by using 4567 as the input for accurate currency conversion.

Available locale

Language Identifier Number Currency
Albanian al + +
Arabic ar + +
Azerbaijani az + +
Belgian French fr_BE + -
Brazilian Portuguese pt_BR + +
Bulgarian bg + +
Czech cs + -
Danish dk + +
Dutch nl + -
English en + +
Estonian et + -
Georgian ka + +
German de + +
French fr + +
Hungarian hu + +
Indonesian id + +
Italian it + -
Kurdish ku + -
Lithuanian lt + +
Latvian lv + +
Macedonian mk + -
Malay ms + +
Persian fa + -
Polish pl + +
Romanian ro + +
Serbian sr + +
Slovak sk + +
Spanish es + +
Russian ru + +
Swedish sv + -
Turkish tr + +
Turkmen tk + +
Ukrainian ua + +
Uzbek uz + +
Yoruba yo + +

Contributors

Some transformers were ported from the pear/Numbers_Words library. Others were created by contributors. Thank you!

Version 2.x - BC and major changes

  • Dropped support for PHP <7.4.
  • Added typehints for NumberTransformer and CurrencyTransformer interfaces. Now both accept integer numbers only (Albanian language might be affected).
  • Added support for PSR12.

Questions and answers

Q: What should I do if I encounter a bug while using the library?

A: If you come across a bug, please open an issue on our GitHub repository. As I may not be proficient in all languages, we encourage users to submit fixes and collaborate to enhance the library's functionality.

Q: My language is missing. Could it be added?

A: There's a high chance I don't know your language. Feel free to implement the missing language and open a pull request. You can use the existing languages as a reference.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.