Dec 23, 2008 - PHP    14 Comments

PHP & MySQL: Unicode number add, subtract etc for any language

Hello Coders!

Recently a visitor from my country asked me how he can add Unicode numbers stored in MySQL database.  I was thinking that it is really a good question. Because you may store any number in any local language to your database and later you may try to add or delete them.

For example, in Bangla language an employee’s salary can be ২৪১৮৬ (which is 24186 in English) bucks and the coder may want to store it in a table under salary field. Another field can be bonus and it can be ২০০ (2000 in English). Now the coder may want to add them up anytime required. You know adding 24186 and 2000 is easy. But what the coder may do in case the values are stored in a local language?

The visitor asked me how he can use the MySQL sum() function in case the data are stored in Unicode format. Well. I do not think MySQL’s unicode will support the sum() function or even if there is one I do not know frankly speaking. So, I thought and developed an alternative solution.

Here is the alternative solution steps:

  1. I get the unicode numbers from the database
  2. Convert them to English number
  3. Sum them up or anything like substract, multiply or divide etc
  4. Convert the result to Unicode once again

Thus, it looks simple procedure. Right?

Before we start I would like to suggest you download the codes from the bottom link of the page and take a look. Because some code may not look as the original one for browser’s case sensitiveness. So, it is better to look at the original code from the download link below.

So,  I wrote a small class to handle the whole procedure and here goes the coding of my class:

If you are not using Bengali language, you need to configure the equivalent number values in convertToEnglishNumber and convertToBanglaNumber functions.

And here goes the page where I am executing the displayTotalPayment() function.

Now the final result looks like this:

Oh! One important thing. If you are coding for European number format, you need to do some modification to handle the comma (,) factor in the number in case required.

Thus, if you use this small tricky method, you can add, subtract,  multiply or divide any local numbers such as Russian, Chinese, Arabic, Spanish, German, Polish, Turkey, Hindi etc.

You can also download this small piece of code from here:
http://www.tanzilo.com/demo/code/unicode_number_add/unicode_number_add.zip

Thank you for reading.

14 Comments

  • Thnx brother, i m really benefited by ur tutorial, thnx a lot. so like this always i need to convert bangla (unicode) to english then whatever i do, like add, sum, sub etc.
    right?

  • @ bill.rassel

    Yeah!
    You are right.

    And you are always welcome.

  • Thanks for this. But, still converting is always need. We can’t do it without conversion 🙂

  • Yeah!
    your idea was good! i need really need this kind of conversion. I use it in nepali unicode language!
    🙂

  • If i wanna input Bengali numbers and then calculate regarding to their values, what are the steps i should flow???
    Thanks for your great tutorial.

  • @ Abdullah Al Mamun

    you should try it yourself
    i am sure you can do it
    if you follow my tutorial closely, you can easily do that
    give a try!

  • vaiya ami ekta PHP5 er bangla ebook- er address chai, jodi keu link dite paren ektu shohojogita korun. pls

  • @ Motahar

    i think there is a PHP book by Suhrid Sorkar in the market. but not sure if there is any ebook version of this book.

  • I would like to thank Almighty that I have found your website. May Almighty help you. It’s really helpful. It really is.

  • ব্যাপারটা হলো এই ফাংশন টা ব্যাবহার করার পর একটা প্রব্লেম এ পরেছি,
    যেটা হলো, আমার ফাংশন ১২ ঘন্টা আগে, ২৯ মিনিট আগে এই টাইপ করে করা, কিন্তু আপনার ফাংশন টা ব্যাবহার করার পর এটা ১২ ঠিক রেখে &#২৬৭৮;&#2৫৩৩;&#২৪৬৫; এমন হয়ে যাচ্ছে, কি করা যায়?

  • @ Raj

    Just debug!
    😛

  • কেমনে করবো এইটাই বলেন ব্রো।

  • @ Raj

    I am working in 5 (five) projects and damn busy!
    So, I cannot offer you much help. Sorry!
    🙁

  • আপনাকে অনেক ধন্যবাদ। অনেক কাজের এই দুটা function । আমার অনেক সময় বেঁচে গেলো।

    The code is working fine with no additional problem.

Got anything to say? Go ahead and leave a comment!