PHP number to word converter using Indian numbering system
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY naveen.pl
4 years ago
Utilities
1
comments comment

I was faced with a requirement to convert number to words/string in Indian naming system with lakhs and crores. I had to generate the amount in words for printing it out on a bill. The total amount, provided to the customer, needed to be converted to string. For example, if 1345673 was to be converted to Thirteen lakh Forty Five Thousand Six Hundred and Seventy Three, these functions will have to be followed.

Code is given below


/**
 * Function to convert a number to a the string literal for the number
 */
function getStringOfAmount($num) {
  $count = 0;
  global $ones, $tens, $triplets;
  $ones = array(
    '',
    ' One',
    ' Two',
    ' Three',
    ' Four',
    ' Five',
    ' Six',
    ' Seven',
    ' Eight',
    ' Nine',
    ' Ten',
    ' Eleven',
    ' Twelve',
    ' Thirteen',
    ' Fourteen',
    ' Fifteen',
    ' Sixteen',
    ' Seventeen',
    ' Eighteen',
    ' Nineteen'
  );
  $tens = array(
    '',
    '',
    ' Twenty',
    ' Thirty',
    ' Forty',
    ' Fifty',
    ' Sixty',
    ' Seventy',
    ' Eighty',
    ' Ninety'
  );

  $triplets = array(
    '',
    ' Thousand',
    ' Million',
    ' Billion',
    ' Trillion',
    ' Quadrillion',
    ' Quintillion',
    ' Sextillion',
    ' Septillion',
    ' Octillion',
    ' Nonillion'
  );
  return convertNum($num);
}

/**
 * Function to dislay tens and ones
 */
function commonloop($val, $str1 = '', $str2 = '') {
  global $ones, $tens;
  $string = '';
  if ($val == 0)
    $string .= $ones[$val];
  else if ($val < 20)
    $string .= $str1.$ones[$val] . $str2;  
  else
    $string .= $str1 . $tens[(int) ($val / 10)] . $ones[$val % 10] . $str2;
  return $string;
}

/**
 * returns the number as an anglicized string
 */
function convertNum($num) {
  $num = (int) $num;    // make sure it's an integer

  if ($num < 0)
    return 'negative' . convertTri(-$num, 0);

  if ($num == 0)
    return 'Zero';
  return convertTri($num, 0);
}

/**
 * recursive fn, converts numbers to words
 */
function convertTri($num, $tri) {
  global $ones, $tens, $triplets, $count;
  $test = $num;
  $count++;
  // chunk the number, ...rxyy
  // init the output string
  $str = '';
  // to display hundred & digits
  if ($count == 1) {
    $r = (int) ($num / 1000);
    $x = ($num / 100) % 10;
    $y = $num % 100;
    // do hundreds
    if ($x > 0) {
      $str = $ones[$x] . ' Hundred';
      // do ones and tens
      $str .= commonloop($y, ' and ', '');
    }
    else if ($r > 0) {
      // do ones and tens
      $str .= commonloop($y, ' and ', '');
    }
    else {
      // do ones and tens
      $str .= commonloop($y);
    }
  }
  // To display lakh and thousands
  else if($count == 2) {
    $r = (int) ($num / 10000);
    $x = ($num / 100) % 100;
    $y = $num % 100;
    $str .= commonloop($x, '', ' Lakh ');
    $str .= commonloop($y);
    if ($str != '')
      $str .= $triplets[$tri];
  }
  // to display till hundred crore
  else if($count == 3) {
    $r = (int) ($num / 1000);
    $x = ($num / 100) % 10;
    $y = $num % 100;
    // do hundreds
    if ($x > 0) {
      $str = $ones[$x] . ' Hundred';
      // do ones and tens
      $str .= commonloop($y,' and ',' Crore ');
    }
    else if ($r > 0) {
      // do ones and tens
      $str .= commonloop($y,' and ',' Crore ');
    }
    else {
      // do ones and tens
      $str .= commonloop($y);
    }
  }
  else {
    $r = (int) ($num / 1000);
  }
  // add triplet modifier only if there
  // is some output to be modified...
  // continue recursing?
  if ($r > 0)
    return convertTri($r, $tri+1) . $str;
  else
    return $str;
}

RELATED ARTICLE

more_horiz
close

on 28th January 2012 / by webmaster
We had sometime back released an Utilities Internet Free Software Shell Scripting Reliance Python Leave a reply Your email address will not be published. Required fields are marker * Praveen Pious (not verified) access_time 17 Jul 2019 - 20:54 Not everyone is well versed with running python scripts on their windows machines. I downloaded and installed python off from their website. I set the environment variable correctly too. Yet, I am facing difficulty while trying to get this script working. Double clicking the script just makes a black command prompt prompt window to appear. Nothing seems to be happening on the background. So, please help me out. What am I doing wrong here? Anonymous (not verified) access_time 17 Jul 2019 - 20:54 i am using a connect broadband. and i don't know how to use a script. please tell me how use it? thanks in advance. Dhruv (not verified) access_time 17 Jul 2019 - 20:54 Even i'm having trouble getting this script working. Can you please tell this in layman terms? Hellobye80 (not verified) access_time 17 Jul 2019 - 20:54 from where can we download this free software. can you please share the link? Ethan Stone (not verified) access_time 17 Jul 2019 - 20:54 Is this script still working for you? Add new comment
close

on 08th December 2014 / by naveen.pl
I was faced with a requirement to convert number to words/string in Indian naming system with lakhs and crores. I had to generate the amount in words for printing it out on a bill. The total amount, provided to the customer, needed to be converted to string. For example, if 1345673 was to be converted to Thirteen lakh Forty Five Thousand Six Hundred and Seventy Three, these functions will have to be followed. Utilities Web Development PHP Leave a reply Your email address will not be published. Required fields are marker * Shridhar Sharma (not verified) access_time 17 Jul 2019 - 13:49 Your function is not working more than once in a php script. Add new comment
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

author-image
Shridhar Sharma (not verified)
access_time 17 Jul 2019 - 20:54

Your function is not working more than once in a php script.