How to sum two Bangla numbers (UTF-8) in PHP?
2 answers
You can't just do it like this, you need to convert them to regular numbers first:
class Converter
{
public static $bn = ["১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯", "০"];
public static $en = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
public static function bn2en($number)
{
return str_replace(self::$bn, self::$en, $number);
}
public static function en2bn($number)
{
return str_replace(self::$en, self::$bn, $number);
}
}
$a = '১২'; //(12)
$b = '৫'; //(5)
$c = Converter::bn2en($a) + Converter::bn2en($b); // $c = 17
echo Converter::en2bn($c); // ১৭
Credit here: http://bits.mdminhazulhaque.io/php/convert-number-between-banlga-and-english-in-php.html
+2
source to share
another approach using an intl
extension:
// create a format from ba local
// you can get all available locales by : print_r(IntlCalendar::getAvailableLocales());
$format = numfmt_create('ba', NumberFormatter::DECIMAL);
$a = numfmt_parse($format, '১২');
$b = numfmt_parse($format, '৫');
echo $c = $a + $b;
Output: 17
+2
source to share