Convert number to words php

You can do this in many ways I am mentioning here two ways by using The NumberFormatter class as mentioned in Martindilling answer [if you have php version 5.3.0 or higher and also PECL extension 1.0.0 or higher] or by using the following custom function.

function convertNumberToWord[$num = false]
{
    $num = str_replace[array[',', ' '], '' , trim[$num]];
    if[! $num] {
        return false;
    }
    $num = [int] $num;
    $words = array[];
    $list1 = array['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
        'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
    ];
    $list2 = array['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred'];
    $list3 = array['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
        'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
        'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
    ];
    $num_length = strlen[$num];
    $levels = [int] [[$num_length + 2] / 3];
    $max_length = $levels * 3;
    $num = substr['00' . $num, -$max_length];
    $num_levels = str_split[$num, 3];
    for [$i = 0; $i < count[$num_levels]; $i++] {
        $levels--;
        $hundreds = [int] [$num_levels[$i] / 100];
        $hundreds = [$hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ' ' : ''];
        $tens = [int] [$num_levels[$i] % 100];
        $singles = '';
        if [ $tens < 20 ] {
            $tens = [$tens ? ' ' . $list1[$tens] . ' ' : '' ];
        } else {
            $tens = [int][$tens / 10];
            $tens = ' ' . $list2[$tens] . ' ';
            $singles = [int] [$num_levels[$i] % 10];
            $singles = ' ' . $list1[$singles] . ' ';
        }
        $words[] = $hundreds . $tens . $singles . [ [ $levels && [ int ] [ $num_levels[$i] ] ] ? ' ' . $list3[$levels] . ' ' : '' ];
    } //end for loop
    $commas = count[$words];
    if [$commas > 1] {
        $commas = $commas - 1;
    }
    return implode[' ', $words];
}

Calculator Use

Convert a number to a US English word representation. Convert a number to USD currency and check writing amounts rounded to 2 decimal places.  Choose to have words for the numbers in lowercase, uppercase or title case to easily copy and paste to another application.

This converter will convert numbers to words and figures to words. The number to words can be done for real numbers and Scientific E Notation. Limited to use of 90 characters and 1e-90 and 1e+90.

References for Number Names

wikipedia.org - Names of large numbers

A PHP function to convert numbers to words. Useful for currency displays, etc.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

How do I print numbers in words in php?

In case of using Yii2 you can do this as simple as the following: $sum = 100500; echo Yii::$app->formatter->asSpellout[$sum]; This prints spelled out $sum in your app's language.

How do I convert numbers to words?

Use the SpellNumber function in individual cells Type the formula =SpellNumber[A1] into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber[22.50]. Press Enter to confirm the formula.

How do you decode numbers to letters?

The Letter-to-Number Cipher [or Number-to-Letter Cipher or numbered alphabet] consists in replacing each letter by its position in the alphabet , for example A=1, B=2, Z=26, hence its over name A1Z26 .

Chủ Đề