Last active
June 2, 2020 16:28
-
-
Save heitara/14ea040dfe7077af9ea76bdd257da340 to your computer and use it in GitHub Desktop.
Number to text, number to words, Словом, Число в текст на български
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
let toBGN = (value) => { | |
let under10 = (value, currency = false) => { | |
let index = Math.floor(value) | |
if(index > 10) { | |
index = 10; | |
} | |
let resultArray = | |
[ | |
"нула", "един", "два", "три", "четири", "пет", "шест", | |
"седем", "осем", "девет", "десет" | |
] | |
if(currency) { | |
let currenciesArrray = | |
[ | |
"лева", "лев" | |
] | |
let i = index == 1 ? 1 : 0 | |
return resultArray[index] + " " + currenciesArrray[0] | |
} | |
return resultArray[index] | |
} | |
let under10BGN = (value) => under10(value, true) | |
let under20 = (value) => { | |
if (value <= 10) return under10(value, true) | |
let index = value - 10 | |
let resultArray = | |
[ | |
"десет","единадесет", "дванадесет", "тринадесет", | |
"четиринадесет", "петнадесет", "шестнадесет", | |
"седемнадесет", "осемнадесет", "деветнадесет", | |
"двадесет" | |
] | |
return resultArray[index] + " " + "лева" | |
} | |
let under100 = (value) => { | |
let intValue = Math.floor(value) | |
let h = Math.floor(intValue / 10) | |
let t = intValue % 10 | |
let resultArray = | |
[ | |
"", "десет", "двадесет", "тридесет", | |
"четиридесет", "петдесет", "шестдесет", | |
"седемдесет", "осемдесет", "деветдесет", | |
"сто" | |
] | |
if(intValue <= 10) { | |
return under10(intValue, true) | |
} else if(intValue <= 20) { | |
return under20(intValue) | |
} | |
let head = "" | |
head = resultArray[h] | |
if(t != 0) { | |
head += " и " + under10(t, true) | |
} else{ | |
head += " лева" | |
} | |
return head | |
} | |
let under1000 = (value) => { | |
let intValue = Math.floor(value) | |
let h = Math.floor(intValue / 100) | |
let t = intValue % 100 | |
let resultArray = | |
[ | |
"", "сто", "двеста", "триста", | |
"четиристотин", "петстотин", "шестстотин", | |
"седемстотин", "осемстотин", "деветстотин", | |
"хиляда" | |
] | |
let head = "" | |
head = resultArray[h] | |
if(intValue <= 100) { | |
return under100(intValue, true) | |
} | |
if(t == 0) { | |
head += " лева" | |
} else if(t <= 20) { | |
head += " и " + under20(t) | |
} else if(Math.floor(t/10) == 0) { | |
head += " и " + under100(t, true) | |
} else { | |
head += " " + under100(t) | |
} | |
return head | |
} | |
let under20000 = (value) => { | |
let intValue = Math.floor(value) | |
let h = Math.floor(intValue / 1000) | |
let t = intValue % 1000 | |
let resultArray = | |
[ | |
"", "хиляда", "две", "три", | |
"четири", "пет", "шест", | |
"седем", "осем", "девет", | |
"десет", ,"единадесет", "дванадесет", "тринадесет", | |
"четиринадесет", "петнадесет", "шестнадесет", | |
"седемнадесет", "осемнадесет", "деветнадесет", | |
"двадесет" | |
] | |
let head = "" | |
head = resultArray[h] | |
if(h > 1) { | |
head += " " + "хиляди" | |
} | |
if(t == 0) { | |
head += " лева" | |
return head | |
} | |
if (t< 100) { | |
head += " и " + under1000(t) | |
} else if(t % 100 == 0 && Math.floor(t / 100) > 0) { | |
head += " и " + under1000(t) | |
} else { | |
head += " " + under1000(t) | |
} | |
return head | |
} | |
let round = Math.floor(value) | |
let tail = Math.round(((value - round) + Number.EPSILON) * 100) | |
let max = [10, 20, 100, 1000, 20000] | |
let functions = [under10BGN, under20, under100, under1000, under20000] | |
let i = 0 | |
while (max[i]< round) { | |
i++ | |
} | |
let t = functions[i](round) | |
let c = "стотинк" + (tail == 1? "а" : "и") | |
if(tail > 0) { | |
return t + " и " + tail + " " + c | |
} | |
return t | |
} | |
//test data | |
// console.log(toBGN(1)); | |
// console.log(toBGN(4)); | |
// console.log(toBGN(14)); | |
// console.log(toBGN(17)); | |
// console.log(toBGN(27)); | |
// console.log(toBGN(97)); | |
// console.log(toBGN(100)); | |
// console.log(toBGN(102)); | |
// console.log(toBGN(112)); | |
// console.log(toBGN(132)); | |
// console.log(toBGN(212)); | |
// console.log(toBGN(302)); | |
// console.log(toBGN(312)); | |
// console.log(toBGN(1000)); | |
// console.log(toBGN(1005)); | |
// console.log(toBGN(1020)); | |
// console.log(toBGN(1300)); | |
// console.log(toBGN(1312)); | |
// console.log(toBGN(1320)); | |
// console.log(toBGN(2352)); | |
// console.log(toBGN(6352)); | |
// console.log(toBGN(7302)); | |
// console.log(toBGN(8152)); | |
// console.log(toBGN(9652)); | |
// console.log(toBGN(9952)); | |
// console.log(toBGN(10000)); | |
// console.log(toBGN(10001)); | |
// console.log(toBGN(10101)); | |
// console.log(toBGN(10020)); | |
// console.log(toBGN(10050)); | |
// console.log(toBGN(10121)); | |
// console.log(toBGN(10300)); | |
// console.log(toBGN(10352)); | |
// console.log(toBGN(1320.32)); | |
// console.log(toBGN(10000.02)); | |
// console.log(toBGN(10001.01)); | |
// console.log(toBGN(10101.12)); | |
// console.log(toBGN(10020.97)); | |
// console.log(toBGN(10050.52)); | |
// console.log(toBGN(10121.90)); | |
// console.log(toBGN(10300.88)); | |
// console.log(toBGN(10352.95)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment