Skip to content

Instantly share code, notes, and snippets.

@stelf
Created August 24, 2017 09:40
Show Gist options
  • Save stelf/95f73f6fe774b714ad5078e513078ac3 to your computer and use it in GitHub Desktop.
Save stelf/95f73f6fe774b714ad5078e513078ac3 to your computer and use it in GitHub Desktop.
function lt1000(number) {
var tensNames = [
"",
" десет",
" двайсет",
" трийсет",
" четирсет",
" петдесет",
" шейсет",
" седемдесет",
" осемдесет",
" деветдесет"
];
var numNames = [
"",
" един",
" два",
" три",
" четири",
" пет",
" шест",
" седем",
" осем",
" девет",
" десет",
" единайсет",
" дванайсет",
" тринайсет",
" четиринайсет",
" петнайсет",
" шестнайсет",
" седемнайсет",
" осемнайсет",
" деветнайсет"
];
var hunNames = [
"", " сто", " двеста", " триста"
];
var current;
if (number % 100 < 20){
current = numNames[number % 100];
number /= 100;
}
else {
current = numNames[number % 10];
number /= 10;
if (current)
current = tensNames[number % 10] + " и "+ current;
else
current = tensNames[number % 10];
number /= 10;
}
if (number == 0) return current;
if (hunNames[number] != undefined) return hunNames[number] + current;
if (current)
return numNames[number] + "стотин и " + current;
else
return numNames[number] + "стотин " + current;
}
public function num2bgn(number) {
var bnum = number * 100; bnum = bnum.castToExactNumber();
if ( bnum / 100 != number) {
var lv = number.castToExactNumber();
var st = ((number * 100).castToExactNumber() ) % 100;
return num2word(lv) + " лева и " + java.math.abs(st) + " стотинки";
} else {
if (number == 1) return " един лев и 00 стотинки";
return num2word(number) + " лева и 00 стотинки";
}
}
public function num2word(number) {
var specialNames = [
"",
" хиляди",
" милиона",
" милиарда",
" трилярда",
" квадрилиона",
" квинтилион"
];
if (number == 0) {
return "нула";
}
var prefix = "";
if (number < 0) {
number = -number;
prefix = "минус";
}
var current = "";
var place = 0;
while (number > 0) {
var n = number % 1000;
if (n != 0) {
var s = lt1000(n);
// console.log([s, n, place, current]);
if (n == 1 && place == 1) {
current = " хиляда " + ( current ? " и " + current : "" );
} else {
current = s + specialNames[place] + ( current ? " и " + current : "" );
}
}
place++;
number = number / 1000;
}
return (prefix + current).trim();
}
function test_num2bgn() {
var nums = [ 1,2,3,-55, -128312.01, 11.33, 303, 3003.05, 300003.55, 11234.90, 21234, 40004.98, 5000055, 5050505.15, 331234.25, 123456];
for (var i: nums) {
console.log(i + " = " + num2bgn(i));
}
}
test_num2bgn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment