Giter VIP home page Giter VIP logo

number-to-words's Introduction

PHP Number to words converter

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 kwn/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 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 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"

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

Language Identifier Number Currency
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 + -
Lithuanian lt + -
Latvian lv + -
Malay ms + -
Polish pl + +
Romanian ro + +
Slovak sk + -
Spanish es + +
Russian ru + +
Swedish sv + -
Turkish tr + +
Turkmen tk + +
Ukrainian ua + +

Contributors

Most of the transformers were ported from pear/Numbers_Words library. Some of them were slightly refactored. Some of them were created by other contributors who were helping me with the initial version of that library.

This library is still under a heavy refactoring so the legacy code should ultimately disappear.

Questions and answers

Q: Why the code looks like a crap? Why it gets so low rank on code climate?

A: Most of the code was just migrated from pear/Numbers_Words. That library still remembers the ages of PHP4. I constantly refactor it, so it's getting better. I'm also porting some mechanisms from other languages so sooner or later it will look quite good.

Q: There's an error!

A: Please report an issue, or even better - create a pull request. I don't speak most of those languages so your help is much appreciated. Thanks!

Q: Why there are some transformers which cannot be used (they live under UntestedLocale namespace)?

A: Simply, because there are no test cases for them. You're more than welcome to create some test cases for them, so we'll be able to include them in a list of available languages.

number-to-words's People

Contributors

kwn avatar zehir avatar janhartigan avatar popovserhii avatar igorzavaleev avatar ickbinhier avatar veguss avatar shahonseven avatar iamdual avatar samnela avatar pudovmaxim avatar andreybolonin avatar scrollup avatar ethmelly avatar vaened avatar coupej avatar havryshkevych avatar wbrframe avatar miansaleem avatar jbdelhommeau avatar goodot avatar dlorunfemi avatar crantodev avatar alvaro-canepa avatar aledeg avatar mangtomas23 avatar nomadzy avatar

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.