How to convert numbers to words in php

How to convert numbers to words in php

Simple function to convert numbers to words using php, this function is capable to convert upto 9 digits

Create a function number_to_words()

function number_to_words($number){
   $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) ?
    "point " . $words[$point / 10] . " " . 
          $words[$point = $point % 10] : '';
  return $result . "  " . $points . " ";
}

Then call function like this

echo number_to_words($number);

View Demo

Leave a reply
Latest Posts
Whatever the Bishnoi community says, it is difficult for Salman Khan to apologize in the black buck case
Whatever the Bishnoi community says, it is difficult for Salman Khan to apologize in the black buck case

Salman Khan has been advised by the Bishnoi community of Rajasthan to apologize in the blackbuck poaching case - but will the matter end if Salman Khan does so? And will Salman Khan want to take this path?

India Mourns the Loss of Beloved Industrial Icon, Padma Vibhushan Ratan Tata
India Mourns the Loss of Beloved Industrial Icon, Padma Vibhushan Ratan Tata

Ratan Tata will be cremated with full state honours after passing away on Wednesday at the age of 86 in a Mumbai hospital. In addition, Eknath Shinde, the chief minister of Maharashtra, declared a day of mourning for the renowned philanthropist and industrialist on Thursday.

Ratan Tata: The Visionary Who Shaped India's Industrial Future
Ratan Tata: The Visionary Who Shaped India's Industrial Future

Ratan Tata, one of the most respected business leaders in India, has left an indelible mark on the nation’s economy through his visionary leadership and values-driven approach. Born on December 28, 1937, into the Tata family, Ratan Tata is the great-grandson of Jamsetji Tata, the founder of the Tata Group, one of India’s oldest and most prestigious conglomerates. Over the decades, Ratan Tata has taken the group to unprecedented heights, earning admiration globally for his business acumen and ethical leadership.

2024 Ballon d'Or Nominees | Top Football Stars Revealed
2024 Ballon d'Or Nominees | Top Football Stars Revealed

The latest news about the 2024 Ballon d'Or nominees. Updates and details on the top football stars. The most current information on football awards at Sportxeber.az.