function toCentsParts(value) { tValue = Math.abs(value); billions = millions = thousands = 0; if (tValue >= 10000000) { billions = Math.floor(tValue / 10000000); tValue = tValue - (billions * 10000000); } if (tValue >= 10000) { millions = Math.floor(tValue / 10000); tValue = tValue - (millions * 10000); } if (tValue >= 10) { thousands = Math.floor(tValue / 10); tValue = tValue - (thousands * 10); } cents = Math.round(tValue * 100); return [billions, millions, thousands, cents]; } function arabicOnesSingle(value) { var cent = value % 100; return cent < 2 || cent > 10; } function getUnitNames(values) { var ar = false; return [ values[0] === 1 || ar && arabicOnesSingle(values[0]) ? 'billion' : 'billions', values[1] === 1 || ar && arabicOnesSingle(values[1]) ? 'million' : 'millions', values[2] === 1 || ar && arabicOnesSingle(values[2]) ? 'thousand' : (ar && values[2] === 2 ? 'two thousands' : 'thousands'), values[3] === 1 || ar && arabicOnesSingle(values[3]) ? 'cent' : 'cents', ]; } function displayCurrencyInWords(value) { var ar = false; var text = ''; var groupConnector = ar ? ' و ' : ', '; [billions, millions, thousands, cents] = toCentsParts(value); [unitBillions, unitMillions, unitThousands, unitCents] = getUnitNames([billions, millions, thousands, cents]); if (billions > 0) text = ((billions === 1 ? '' : billions + ' ') + unitBillions); if (millions > 0) text += (text ? groupConnector : '') + ((millions === 1 ? '' : millions + ' ') + unitMillions); if (thousands > 0) text += (text ? groupConnector : '') + ((thousands === 1 || (ar && thousands === 2) ? '' : thousands + ' ') + unitThousands); if (cents > 0) text += (text ? groupConnector : '') + ((cents === 1 ? '' : cents + ' ') + unitCents); return text; } function displayCurrency(x) { var thousandSep = ' '; x = x.toFixed(2).toString().replace('.', ','); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x)) x = x.replace(pattern, "$1" + thousandSep + "$2"); return x + ' DA'; } function redirect(url) { window.location = url; }