Last active
October 6, 2024 11:05
-
-
Save bymustfa/e35de3dcd975710b2f91959776a248dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numToWord(number) { | |
const ones = ['', 'bir', 'iki', 'üç', 'dört', 'beş', 'altı', 'yedi', 'sekiz', 'dokuz']; | |
const tens = ['', 'on', 'yirmi', 'otuz', 'kırk', 'elli', 'altmış', 'yetmiş', 'seksen', 'doksan']; | |
const scales = ['', 'bin', 'milyon', 'milyar', 'trilyon', 'katrilyon']; | |
let integerPart = Math.floor(number); | |
let fractionalPart = Math.round((number - integerPart) * 100); // Kuruş için iki basamak | |
let result = ''; | |
function convertLessThanThousand(num) { | |
let str = ''; | |
if (num >= 100) { | |
let hundreds = Math.floor(num / 100); | |
if (hundreds > 1) { | |
str += ones[hundreds] + ' yüz '; | |
} else { | |
str += 'yüz '; | |
} | |
num = num % 100; | |
} | |
if (num >= 20) { | |
let tensDigit = Math.floor(num / 10); | |
str += tens[tensDigit] + ' '; | |
num = num % 10; | |
} else if (num >= 10) { | |
str += tens[1] + ' '; // 'on' | |
num = num % 10; | |
} | |
if (num > 0) { | |
str += ones[num] + ' '; | |
} | |
return str.trim(); | |
} | |
let integerStr = ''; | |
if (integerPart === 0) { | |
integerStr = 'sıfır'; | |
} else { | |
let scaleCounter = 0; | |
while (integerPart > 0) { | |
let chunk = integerPart % 1000; | |
if (chunk !== 0) { | |
let chunkStr = convertLessThanThousand(chunk); | |
let scaleName = scales[scaleCounter]; | |
if (scaleName === 'bin' && chunk === 1) { | |
chunkStr = 'bin'; | |
} else if (scaleCounter > 0) { | |
chunkStr += ' ' + scaleName; | |
} | |
integerStr = chunkStr + ' ' + integerStr; | |
} | |
integerPart = Math.floor(integerPart / 1000); | |
scaleCounter++; | |
} | |
result = integerStr.trim(); | |
} | |
let fractionalStr = ''; | |
if (fractionalPart > 0) { | |
fractionalStr = convertLessThanThousand(fractionalPart); | |
} | |
let finalResult = ''; | |
if (result && fractionalStr) { | |
finalResult = result + ' lira ' + fractionalStr + ' kuruş'; | |
} else if (result) { | |
finalResult = result + ' lira'; | |
} else if (fractionalStr) { | |
finalResult = fractionalStr + ' kuruş'; | |
} else { | |
finalResult = 'Sıfır lira'; | |
} | |
finalResult = finalResult.replace(/\s+/g, ' ').trim(); | |
finalResult = finalResult.charAt(0).toUpperCase() + finalResult.slice(1); | |
return finalResult; | |
} | |
console.log(numToWord(1346780.12)); // Bir milyon üç yüz kırk altı bin yedi yüz seksen lira on iki kuruş | |
console.log(numToWord(17.99)); // On yedi lira doksan dokuz kuruş | |
console.log(numToWord(159357852654.75)); // Yüz elli dokuz milyar üç yüz elli yedi milyon sekiz yüz elli iki bin altı yüz elli dört lira yetmiş beş kuruş | |
console.log(numToWord(0.05)); // Beş kuruş | |
console.log(numToWord(1001.01)); // Bin bir lira bir kuruş | |
console.log(numToWord(10)); // On lira |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment