convertTo

convert numbers to farsi, english, arabic.

convert numbers to farsi, english, arabic. تبدیل عداد به فارسی, انگلیسی, عربی

Plug-in code

$(function () {
    $.fn.dataTable.numberTo = function (numbers, to = 'fa') {
        let result = null;
        const faNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
        const enNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        const arNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

        if (!numbers && to === 'fa') {
            return 'مقدار ورودی صحیح نمی‌باشد.'
        } else if (!numbers) {
            return 'numbers is empty.'
        }

        switch (to) {
            case 'fa':
                result = numbers.toString().replace(/\d/g, x => faNumbers[x]);
                break;
            case 'en':
                result = numbers.toString().replace(/\d/g, x => enNumbers[x]);
                break;
            case 'ar':
                result = numbers.toString().replace(/\d/g, x => arNumbers[x]);
                break;
        }
        return result;
    };
})

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$('#example').DataTable( {
     columnDefs: [
       {
             data: 'id',
             name: 'id',
             fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                 $(nTd).html($.fn.dataTable.numberTo(oData.id, 'fa'));
             }
       },
     ]
  } );