2018-12-02 Pujan Niroula 2 Minute(s) Read

Calculate Devanagari(Nepali/Hindi) Numbers in PHP

img-devanagari number php.png

Hello there,

Specially if you are developing web apps for local Nepali or Indian offices with feature of calculation then you might have to display, calculate and manage Devanagari numbers. Today I will show you how to manage, calculate and convert default numbers to Devanagari Numbers and vice-versa.

Convert English Numbers to Devanagari Numbers in PHP

Let's convert nnumbers to Devanagari Numbers. You can simply use str_replace() function of PHP. Simply str_replace() expects at least 3 parameters. First parameter is Search, Second is Replace and third one is Subject. It means this function search for first parameter in given subject and replace it with second parameter.

<?php
function devanagari($num)
{
   $num_nepali = array('०','१','२','३','४','५','६','७','८','९');
   $num_eng = array('0','1','2','3','4','5','6','7','8','9');
   $nums = str_replace($num_eng, $num_nepali, $num);
   return $nums;
}
echo devanagari('13905'); //Output १३९०५
?>

Convert Devanagari Numbers to English Numbers in PHP

If you have to manage and calculate Devanagari numbers then what will you do? Simple convert it to English numbers and manage and calculate it. Right? Okay, than lets do...

<?php
function english($num)
{
   $num_nepali = array('०','१','२','३','४','५','६','७','८','९');
   $num_eng = array('0','1','2','3','4','5','6','7','8','9');
   $nums = str_replace($num_nepali, $num_eng, $num);
   return $nums;
}
echo english('१३९०५'); //Output 13905
?>

How to Calculate Devanagari Numbers

Simple just convert Devanagari numbers to English numbers then calculate it in regular ways. Finally print out the result of calculation in Devanagari font. Let me to show you an example.

<?php
function devanagari($num)
{
   $num_nepali = array('०','१','२','३','४','५','६','७','८','९');
   $num_eng = array('0','1','2','3','4','5','6','7','8','9');
   $nums = str_replace($num_eng, $num_nepali, $num);
   return $nums;
}
function english($num)
{
   $num_nepali = array('०','१','२','३','४','५','६','७','८','९');
   $num_eng = array('0','1','2','3','4','5','6','7','8','9');
   $nums = str_replace($num_nepali, $num_eng, $num);
   return $nums;
}
$input = '१२०००';
$input2 = '१२';
$eng_input = english($input);
$eng_input2 = english($input2);
$result = $eng_input/$eng_input2;
echo devanagari($result);//output १०००
?>

That's all, this is how to calculate and convert Devanagari Numbers in PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Cheers!!!

Download Attached Files: Devanagari Number Converter in PHP.zip
Comments (2) Add New Comment
LewisRar2024-06-10 8:45 AM

whoah this weblog is great i really like studying your articles. Stay up the great work! You know, many individuals are hunting round for this information, you could help them greatly. forum.meha.biz/topic/1071/?  kids-news.ru/page/3?  fincasanlorenzo.es/sitemap/?  newsbeautiful.ru/page/10/?  bn.tobase.ru/viewtopic.php?f=30&t=2717? 

Reply
Barala2022-04-26 11:43 AM

Devanagari number follows Devanagari script

Reply