LaravelPHPProgramming

Convert numbers to words in laravel

Hello buddy, I hope you are doing well, In this article we will learn how convert number to words in laravel, as well as how to convert number to words in PHP.

it’s a simple example of laravel convert number to words. you’ll learn how to convert number to words in laravel blade example. Let’s get started with how to convert number to words in laravel.

Sometimes it’s necessary to convert kind of number like 1143 to one thousand one hundred and forty-three, during the invoice, payroll slip, etc in laravel.which means we have to convert number into the words.

Convert numbers into words in laravel

$totalAmount = '1143';
        $amountInWords = ucwords((new NumberFormatter('en_IN', NumberFormatter::SPELLOUT))->format($totalAmount));

The above code will give the output like this one One Thousand One Hundred And Forty-Three. Now we will see how to do things in PHP.

Convert numbers into words in PHP

<?php
  /**
   * Created by PhpStorm.
   * Converting Currency Numbers to words currency format
   */
$number = 190908100.25;
   $no = floor($number);
   $point = round($number - $no, 2) * 100;
   $hundred = null;
   $digits_1 = strlen($no);
   $i = 0;
   $str = array();
   $words = array('0' => '', '1' => 'one', '2' => 'two',
    '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
    '7' => 'seven', '8' => 'eight', '9' => 'nine',
    '10' => 'ten', '11' => 'eleven', '12' => 'twelve',
    '13' => 'thirteen', '14' => 'fourteen',
    '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
    '18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
    '30' => 'thirty', '40' => 'forty', '50' => 'fifty',
    '60' => 'sixty', '70' => 'seventy',
    '80' => 'eighty', '90' => 'ninety');
   $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
   while ($i < $digits_1) {
     $divider = ($i == 2) ? 10 : 100;
     $number = floor($no % $divider);
     $no = floor($no / $divider);
     $i += ($divider == 10) ? 1 : 2;
     if ($number) {
        $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
        $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
        $str [] = ($number < 21) ? $words[$number] .
            " " . $digits[$counter] . $plural . " " . $hundred
            :
            $words[floor($number / 10) * 10]
            . " " . $words[$number % 10] . " "
            . $digits[$counter] . $plural . " " . $hundred;
     } else $str[] = null;
  }
  $str = array_reverse($str);
  $result = implode('', $str);
  $points = ($point) ?
    "." . $words[$point / 10] . " " . 
          $words[$point = $point % 10] : '';
  echo $result . "Rupees  " . $points . " Paise";
 ?> 

The above code will give the output like this one nineteen crores nine lakh eight thousand one hundred Rupees . two five Paise.

Now, I think you have the idea of how to convert numbers into words in Laravel as well as in the PHP.

If your problem is solved by reading this blog, don’t forget to rate us.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button