Skip to content

Instantly share code, notes, and snippets.

@buglss
Last active June 10, 2022 13:45
Show Gist options
  • Save buglss/84fbbc1a712fcae3cc543d8ad767417b to your computer and use it in GitHub Desktop.
Save buglss/84fbbc1a712fcae3cc543d8ad767417b to your computer and use it in GitHub Desktop.
Girilen sayıyı basamaklarına ayırıp metinsel karşılığını dönen fonksiyon. / Function that splits the entered number into its digits and returns its textual equivalent.
const numberToTextLookup = [
{ "0": "Sıfır", "1": "Yüz", "2": "İkiyüz", "3": "Üçyüz", "4": "Dörtyüz", "5": "Beşyüz", "6": "Altıyüz", "7": "Yediyüz", "8": "Sekizyüz", "9": "Dokuzyüz", "undefined": " " },
{ "0": "Sıfır", "1": "On", "2": "Yirmi", "3": "Otuz", "4": "Kırk", "5": "Elli", "6": "Atmış", "7": "Yetmiş", "8": "Seksen", "9": "Doksan", "undefined": " " },
{ "0": "Sıfır", "1": "Bir", "2": "İki", "3": "Üç", "4": "Dört", "5": "Beş", "6": "Altı", "7": "Yedi", "8": "Sekiz", "9": "Dokuz", "undefined": " " }
]
function numberToText(number, digit = 3) {
number = +number
if(isNaN(number) || digit > 3 || digit < 0) return ""
let textBuf = []
for(let section of number.toString().split(RegExp(`(\\d{${digit}})`, "g")).filter(Boolean))
for(let lookup = numberToTextLookup.slice(-digit), n = section.length, lookupIndex = digit - n, buf = [], digitIndex = 0; lookupIndex < digit; digitIndex++, lookupIndex++, (lookupIndex === digit && (textBuf.push(buf.join(" ")), buf = [])))
(digitIndex !== 0 && section[digitIndex] == 0) || buf.push(lookup[lookupIndex][section[digitIndex]])
return textBuf.join(" ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment