Last active
September 28, 2021 17:15
-
-
Save bojidaryovchev/b9c71d9df4d00f2158b4795fd2eb9d98 to your computer and use it in GitHub Desktop.
This file contains 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
const masculineDigits = ['нула', 'един', 'два', 'три', 'четири', 'пет', 'шест', 'седем', 'осем', 'девет', 'десет']; | |
const feminineDigits = ['нула', 'една', 'две', 'три', 'четири', 'пет', 'шест', 'седем', 'осем', 'девет', 'десет']; | |
const teens = ['единайсет', 'дванайсет', 'тринайсет', 'четиринайсет', 'петнайсет', 'шестнайсет', 'седемнайсет', 'осемнайсет', 'деветнайсет'] | |
const decimals = ['десет', 'двайсет', 'трийсет', 'четиридесет', 'петдесет', 'шейсет', 'седемдесет', 'осемдесет', 'деветдесет']; | |
const hundreds = ['сто', 'двеста', 'триста', 'четиристотин', 'петстотин', 'шестстотин', 'седемстотин', 'осемстотин', 'деветстотин']; | |
const thousands = ['хиляда', 'две хиляди', 'три хиляди', 'четири хиляди', 'пет хиляди', 'шест хиляди', 'седем хиляди', 'осем хиляди', 'девет хиляди']; | |
const moneySingular = 'лев'; | |
const moneyPlural = 'лева'; | |
const cent = 'стотинка'; | |
const cents = 'стотинки'; | |
function moneyAmountToMoney(moneyAmount, recursion) { | |
if (moneyAmount >= 1 && moneyAmount <= 10) { | |
const index = moneyAmount; | |
return `${masculineDigits[index]} ${ index === 1 && !recursion ? moneySingular : moneyPlural}`; | |
} else if (moneyAmount >= 11 && moneyAmount <= 19) { | |
const index = (moneyAmount % 10) - 1; | |
return `${teens[index]} ${moneyPlural}`; | |
} else if (moneyAmount >= 20 && moneyAmount <= 99) { | |
const [decimalIndex, digitIndex] = (moneyAmount / 10).toString().split('.'); | |
if (masculineDigits[digitIndex]) { | |
return `${decimals[decimalIndex - 1]} и ${masculineDigits[digitIndex]} ${moneyPlural}`; | |
} else { | |
return `${decimals[decimalIndex - 1]} ${moneyPlural}`; | |
} | |
} else if (moneyAmount >= 100 && moneyAmount <= 999) { | |
let [hundredIndex, remainingAmount] = (moneyAmount / 100).toString().split('.'); | |
if (remainingAmount) { | |
remainingAmount = moneyAmount % 100; | |
const useAnd = (remainingAmount / 10).toString().split('.').length === 1; | |
return `${hundreds[hundredIndex - 1]}${useAnd ? ' и ' : ' '}${moneyAmountToMoney(remainingAmount, true)}`; | |
} else { | |
return `${hundreds[hundredIndex - 1]} ${moneyPlural}`; | |
} | |
} else if (moneyAmount >= 1000 && moneyAmount <= 9999) { | |
let [thousandIndex, remainingAmount] = (moneyAmount / 1000).toString().split('.'); | |
if (remainingAmount) { | |
remainingAmount = moneyAmount % 1000; | |
const useAnd = (remainingAmount / 100).toString().split('.').length === 1; | |
return `${thousands[thousandIndex - 1]}${useAnd ? ' и ' : ' '}${moneyAmountToMoney(remainingAmount, true)}`; | |
} else { | |
return `${thousands[thousandIndex - 1]} ${moneyPlural}`; | |
} | |
} | |
} | |
function moneyAmountToCents(moneyAmount) { | |
if (moneyAmount >= 1 && moneyAmount <= 10) { | |
const index = moneyAmount; | |
return `${feminineDigits[index]} ${ index === 1 ? cent : cents}`; | |
} else if (moneyAmount >= 11 && moneyAmount <= 19) { | |
const index = (moneyAmount % 10) - 1; | |
return `${teens[index]} ${cents}`; | |
} else if (moneyAmount >= 20 && moneyAmount <= 99) { | |
const [decimalIndex, digitIndex] = (moneyAmount / 10).toString().split('.'); | |
if (feminineDigits[digitIndex]) { | |
return `${decimals[decimalIndex - 1]} и ${feminineDigits[digitIndex]} ${cents}`; | |
} else { | |
return `${decimals[decimalIndex - 1]} ${cents}`; | |
} | |
} | |
} | |
function moneyAmountToBulgarian(moneyAmount) { | |
if (moneyAmount <= 0) { | |
throw new Error('The provided money amount must be positive'); | |
} | |
// if there are two parts it means '.' exists and the number is a floating one | |
const hasFloatingPoint = `${moneyAmount}`.split('.').length === 2; | |
if (hasFloatingPoint) { | |
// there cannot be more than 99 cents therefore we need to round up to the second digit | |
moneyAmount = moneyAmount.toFixed(2); | |
const [money, cents] = `${moneyAmount}`.split('.').map(s => +s); | |
if (!money) { | |
return moneyAmountToCents(cents); | |
} | |
return `${moneyAmountToMoney(money)} и ${moneyAmountToCents(cents)}`; | |
} else { | |
return moneyAmountToMoney(moneyAmount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment