Skip to content

Instantly share code, notes, and snippets.

@Merazsohel
Created November 24, 2021 07:42
Show Gist options
  • Save Merazsohel/5b9acb6ef80a2d4e09f07197e405b98c to your computer and use it in GitHub Desktop.
Save Merazsohel/5b9acb6ef80a2d4e09f07197e405b98c to your computer and use it in GitHub Desktop.
(English to Bangla) money value to words
var IS_SOUTH_ASIAN = true;
var ONES_WORD = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
var TENS_WORD = ['', '', 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
var SCALE_WORD_WESTERN = ['', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion', 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion'];
var SCALE_WORD_SOUTH_ASIAN = ['', 'Thousand', 'Lakh', 'Crore', 'Hundred Crore', 'Thousand Crore', 'Lakh Crore', 'Crore Crore', 'Hundred Crore Crore', '***', '***'];
var GROUP_SIZE = (typeof IS_SOUTH_ASIAN != "undefined" && IS_SOUTH_ASIAN) ? 2 : 3;
var SCALE_WORD = (typeof IS_SOUTH_ASIAN != "undefined" && IS_SOUTH_ASIAN) ? SCALE_WORD_SOUTH_ASIAN : SCALE_WORD_WESTERN;
function int_to_words(int) {
if (int === 0) return 'Zero';
return recurse('', 0, get_first_3(String(int)), get_rest_3(String(int)));
}
// Return string of first three digits, padded with zeros if needed
function get_first_3(str) {
return ('000' + str).substr(-(3));
}
function get_first(str) { //-- Return string of first GROUP_SIZE digits, padded with zeros if needed, if group size is 2, make it size 3 by prefixing with a '0'
return (GROUP_SIZE == 2 ? '0' : '') + ('000' + str).substr(-(GROUP_SIZE));
}
// Return string of digits with first three digits chopped off
function get_rest_3(str) {
return str.substr(0, str.length - 3);
}
function get_rest(str) { // Return string of digits with first GROUP_SIZE digits chopped off
return str.substr(0, str.length - GROUP_SIZE);
}
// Return string of triplet convereted to words
function triplet_to_words(_3rd, _2nd, _1st) {
return (_3rd == '0' ? '' : ONES_WORD[_3rd] + ' Hundred ') +
(_1st == '0' ? TENS_WORD[_2nd] : TENS_WORD[_2nd] && TENS_WORD[_2nd] + ' ' || '') +
(ONES_WORD[_2nd + _1st] || ONES_WORD[_1st]); //-- 1st one returns one-nineteen - second one returns one-nine
}
// Add to result, triplet words with scale word
function add_to_result(result, triplet_words, scale_word) {
return triplet_words ? triplet_words + (scale_word && ' ' + scale_word || ' ') + ' ' + result : result;
}
function recurse(result, scaleIdx, first, rest) {
if (first == '000' && rest.length === 0) return result + 'Taka Only.';
var newResult = add_to_result(result, triplet_to_words(first[0], first[1], first[2]), SCALE_WORD[scaleIdx]);
return recurse(newResult, ++scaleIdx, get_first(rest), get_rest(rest));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment