Number Translation using PHP & JavaScript

Are you having problem with translating numbers to their equivalent signs in different languages? Don’t worry! This post helps you in number translation.

Before I begin, lets talk little about numeric representation of numbers.

The most common system for the symbolic representation of numbers are Arabic numerals (also called Hindu-Arabic numerals) – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Most of the languages, including English use Arabic numerals. In some other languages, Arabic numerals are more common than the native character sets. However, there are still many languages which use their own native character sets to represent numbers.

As an example, I will discuss how to convert English numbers to Nepali (Devanagari) equivalent and vice versa using PHP. Furthermore, I will show how to convert using JavaScript.

Number Translation using PHP

We can easily convert Standard (English) Numbers to equivalent character sets using simple replace operation. The following PHP function converts Standard numbers to Devanagari Unicode numbers. You can get numbers in other languages by simply replacing the array.

<?php
  
  /**
    * @desc Converts Standard Number to Devanagari Unicode (nepali, hindi)
    * 
    * @param integer $input 
    * @return string 
    */
  function convertToUnicodeNumber($input) {
    
    $standard_numsets = array("0","1","2","3","4","5","6","7","8","9");
    $devanagari_numsets = array("०","१","२","३","४","५","६","७","८","९");
    
    return str_replace($standard_numsets, $devanagari_numsets, $input);

  }
  
  /* Usage */
  echo convertToUnicodeNumber(1234); // Displays १२३४

?>

Similarly, we can convert Unicode numbers to Standard English numbers by simply switching parameters in above str_replace function. Here’s the snippet.

<?php
  /**
    * @desc Converts Unicode Number to Standard English Number
    * @param string $input 
    * @return string 
    */
  function convertToStandardNumber($input) {
    
    $standard_numsets = array("0","1","2","3","4","5","6","7","8","9");
    $devanagari_numsets = array("०","१","२","३","४","५","६","७","८","९");
    
    return str_replace($devanagari_numsets, $standard_numsets, $input);

  }
  
  /* Usage */
  echo convertToStandardNumber('१२३४'); // Displays 1234

?>

So far, I have illustrated how to do simple number translation using PHP. Now lets do it using JavaScript.

Number Translation using JavaScript

In JavaScript, we can convert numeric character by simply using replace function. The following code illustrates number translation using JS.

<script type="text/javascript">

  var englishDigits = {
    '०': '0',
    '१': '1',
    '२': '2',
    '३': '3',
    '४': '4',
    '५': '5',
    '६': '6',
    '७': '7',
    '८': '8',
    '९': '9'
  };


  var devanagariDigits = {
    '0': '०',
    '1': '१',
    '2': '२',
    '3': '३',
    '4': '४',
    '5': '५',
    '6': '६',
    '7': '७',
    '8': '८',
    '9': '९'
  };

  var amount = 9857;
  
  /* Converting English number to Devangari (js) */
  var amount_np = amount.toString().replace(/[0123456789]/g, function(s) {
                    return devanagariDigits[s];
                });
                
  console.log(amount_np); // Output: ९८५७  
           
    
                  
  /* Converting Devanagari number to English (js) */
  var amount_en = amount_np.replace(/[०१२३४५६७८९]/g, function(s) {
                  return englishDigits[s];
              });  
        
  console.log(amount_en); // Output 9857           


</script> 

In this way, we can achieve number conversion with PHP and JavaScript.

 

Leave a Reply