Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
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
You can’t perform that action at this time.