This topic is locked

Guide 20 - Transform Numbers to Words

6/26/2021 10:55:28 AM
PHPRunner Tips and Tricks
fhumanes author

As functions of converting numbers to words is a classic in programming.

In this case, a PHPrunner developer requests me an example that allows you to turn numbers to words (English with dollars) and from that request I started looking at GitHub functions made in PHP that I would provide me with that functionality.

I have found many codes, but with very different quality. It is true that I have sought those who are capable of transforming words into several languages (English, Spanish, etc.) because this way as an example that served more developers.

In the end I have used:

  1. https://github.com/kwn/number-to-words (The one I like the most)
  2. https://github.com/egy1st/Number2Text (The simplest to implement)

DEMOS:

  1. [https://fhumanes.com/number-to-words/](https://fhumanes.com/number-to-words/)
  2. https://fhumanes.com/numbers2words/

All the information and examples to be able to download them in my portal.

fhumanes author 6/26/2021

DEMOS:

  1. https://fhumanes.com/number-to-words/
  2. https://fhumanes.com/numbers2words/

img alt

This is the aspect of the example. In general it is a figure (2 decimals) and a language (the products come out) and the example, transforms that number to the words that describe the amount.

In the case of classes of option 1 you can get an error, that's because that class has an option for coins and another that I have not put the example with currency and in that case not all languages are implemented, but It is easy to solve these problems (write me if you can not solve it).

Coding is very simple. In the case of class (1):

<?php

require __DIR__ . '/number-to-words_1.13/autoload.php';

use NumberToWords\NumberToWords;

$amount = $values['amount'];
$language = $values['language'];

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

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

// $values['text'] = $numberTransformer->toWords($amount);

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

$values['text'] = $currencyTransformer->toWords(($amount*100),'USD');

In the case of class (2):

require_once __DIR__ . '/../MyCode/num2text/Num2Text.php';

//create an instance of Number2Text
$oTextNum = New Num2Text() ;

// set currency names
$oTextNum->setCurrency("'coin'", "'coins'", "'cent'", "'cents'"); // put the corresponding currency

// send request
$number = $values['amount'];
$language = $values['language'];
$values['text'] = $oTextNum->translateNumber($number, $language) ;

As always you indicated, for any questions or problems, please contact through my email fernandohumanes@gmail.com.

I leave you, so you can install them in your PC, the 2 examples. You must select one for your project. As I indicated, I like (1), because in Spanish the transformation is much better.