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 (4) Add New Comment
Ismaeleromo2024-06-23 5:49 PM

Heya this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding experience so I wanted to get advice from someone with experience. Any help would be enormously appreciated! linkheed.com/blogs/920/Why-is-the-demand-and-popularity-of-universities-declining-now  templateinspire.com/opencart/Lingerie/index.php?route=information/blogger&blogger_id=2  kanc4life.com.ua/index.php?links_exchange=yes&page=170&show_all=yes  dom-postroj.ru/doma-iz-gazobetona/k-231  slcomp.kz/index.php?route=information/blogger&blogger_id=5 

Reply
Ismaeleromo2024-06-22 8:19 AM

Hey there! Do you use Twitter? I'd like to follow you if that would be ok. I'm absolutely enjoying your blog and look forward to new posts. mymotospeed.ru/page/4  dollarafont.com/contact.html  myworldoftank.ru/article/kak-vklyuchit-otobrazhenie-shansov-na-pobedu-xvm-v-world-of-tanks.html  krutovo.ru/index.php?name=Account&op=info&uname=ymonozav  mangopik.com/sximodemo7/posts/read/printing-and-typesetting-industry 

Reply
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