moneytostr
Version:
The utility class to convert amount to words in Russian/Ukrainian/English languages
1 lines • 17 kB
JavaScript
var currencyList={CurrencyList:{language:{"-value":"UKR"},UKR:{item:[{"-value":"minus","-text":"мінус"},{"-value":"0","-text":"нуль"},{"-value":"1000_10","-text":"тисяч,мільйонів,мільярдів,трильйонів"},{"-value":"1000_1","-text":"тисяча,мільйон,мільярд,трильйон"},{"-value":"1000_234","-text":"тисячі,мільйона,мільярда,трильйона"},{"-value":"1000_5","-text":"тисяч,мільйонів,мільярдів,трильйонів"},{"-value":"10_19","-text":"десять,одинадцять,дванадцять,тринадцять,чотирнадцять,п’ятнадцять,шiстнадцять,сiмнадцять,вiсiмнадцять,дев'ятнадцять"},{"-value":"1","-text":"одна,один,один,одна"},{"-value":"2","-text":"дві,два,два,дві"},{"-value":"3_9","-text":"три,чотири,п’ять,шість,сім,вісім,дев’ять"},{"-value":"100_900","-text":"сто ,двісті ,триста ,чотириста ,п’ятсот ,шістсот ,сімсот ,вісімсот ,дев’ятсот "},{"-value":"20_90","-text":"двадцять ,тридцять ,сорок ,п’ятдесят ,шістдесят ,сімдесят ,вісімдесят ,дев’яносто "},{"-value":"pdv","-text":"в т.ч. ПДВ "},{"-value":"pdv_value","-text":"20"}]},RUS:{item:[{"-value":"minus","-text":"минус"},{"-value":"0","-text":"ноль"},{"-value":"1000_10","-text":"тысяч,миллионов,миллиардов,триллионов"},{"-value":"1000_1","-text":"тысяча,миллион,миллиард,триллион"},{"-value":"1000_234","-text":"тысячи,миллиона,миллиарда,триллиона"},{"-value":"1000_5","-text":"тысяч,миллионов,миллиардов,триллионов"},{"-value":"10_19","-text":"десять,одиннадцать,двенадцать,тринадцать,четырнадцать,пятнадцать,шестнадцать,семнадцать,восемнадцать,девятнадцать"},{"-value":"1","-text":"одна,один,один,одна"},{"-value":"2","-text":"две,два,два,две"},{"-value":"3_9","-text":"три,четыре,пять,шесть,семь,восемь,девять"},{"-value":"100_900","-text":"сто ,двести ,триста ,четыреста ,пятьсот ,шестьсот ,семьсот ,восемьсот ,девятьсот "},{"-value":"20_90","-text":"двадцать ,тридцать ,сорок ,пятьдесят ,шестьдесят ,семьдесят ,восемьдесят ,девяносто "},{"-value":"pdv","-text":"в т.ч. НДС "},{"-value":"pdv_value","-text":"18"}]},ENG:{item:[{"-value":"minus","-text":"minus"},{"-value":"0","-text":"zero"},{"-value":"1000_10","-text":"thousand,million,billion,trillion"},{"-value":"1000_1","-text":"thousand,million,billion,trillion"},{"-value":"1000_234","-text":"thousand,million,billion,trillion"},{"-value":"1000_5","-text":"thousand,million,billion,trillion"},{"-value":"10_19","-text":"ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen"},{"-value":"1","-text":"one,one,one,one"},{"-value":"2","-text":"two,two,two,two"},{"-value":"3_9","-text":"three,four,five,six,seven,eight,nine"},{"-value":"100_900","-text":"one hundred ,two hundred ,three hundred ,four hundred ,five hundred ,six hundred ,seven hundred ,eight hundred ,nine hundred "},{"-value":"20_90","-text":"twenty-,thirty-,forty-,fifty-,sixty-,seventy-,eighty-,ninety-"},{"-value":"pdv","-text":"including VAT "},{"-value":"pdv_value","-text":"10"}]},RUR:[{"-CurrID":"810","-CurrName":"Российские рубли","-language":"RUS","-RubOneUnit":"рубль","-RubTwoUnit":"рубля","-RubFiveUnit":"рублей","-RubSex":"M","-RubShortUnit":"руб.","-KopOneUnit":"копейка","-KopTwoUnit":"копейки","-KopFiveUnit":"копеек","-KopSex":"F"},{"-CurrID":"810","-CurrName":"Российские рубли","-language":"UKR","-RubOneUnit":"рубль","-RubTwoUnit":"рублі","-RubFiveUnit":"рублів","-RubSex":"M","-RubShortUnit":"руб.","-KopOneUnit":"копійка","-KopTwoUnit":"копійки","-KopFiveUnit":"копійок","-KopSex":"F"},{"-CurrID":"810","-CurrName":"Российские рубли","-language":"ENG","-RubOneUnit":"ruble","-RubTwoUnit":"rubles","-RubFiveUnit":"rubles","-RubSex":"M","-RubShortUnit":"RUR.","-KopOneUnit":"kopeck","-KopTwoUnit":"kopecks","-KopFiveUnit":"kopecks","-KopSex":"M"}],UAH:[{"-CurrID":"980","-CurrName":"Украинскі гривні","-language":"RUS","-RubOneUnit":"гривня","-RubTwoUnit":"гривни","-RubFiveUnit":"гривень","-RubSex":"F","-RubShortUnit":"грн.","-KopOneUnit":"копейка","-KopTwoUnit":"копейки","-KopFiveUnit":"копеек","-KopSex":"F"},{"-CurrID":"980","-CurrName":"Украинскі гривні","-language":"UKR","-RubOneUnit":"гривня","-RubTwoUnit":"гривні","-RubFiveUnit":"гривень","-RubSex":"F","-RubShortUnit":"грн.","-KopOneUnit":"копійка","-KopTwoUnit":"копійки","-KopFiveUnit":"копійок","-KopSex":"F"},{"-CurrID":"980","-CurrName":"Украинскі гривні","-language":"ENG","-RubOneUnit":"hryvnia","-RubTwoUnit":"hryvnias","-RubFiveUnit":"hryvnias","-RubSex":"M","-RubShortUnit":"UAH.","-KopOneUnit":"kopeck","-KopTwoUnit":"kopecks","-KopFiveUnit":"kopecks","-KopSex":"M"}],USD:[{"-CurrID":"840","-CurrName":"Долари США","-language":"RUS","-RubOneUnit":"доллар","-RubTwoUnit":"доллара","-RubFiveUnit":"долларов","-RubSex":"M","-RubShortUnit":"дол.","-KopOneUnit":"цент","-KopTwoUnit":"цента","-KopFiveUnit":"центов","-KopSex":"M"},{"-CurrID":"840","-CurrName":"Долари США","-language":"UKR","-RubOneUnit":"долар","-RubTwoUnit":"долара","-RubFiveUnit":"доларів","-RubSex":"M","-RubShortUnit":"дол.","-KopOneUnit":"цент","-KopTwoUnit":"цента","-KopFiveUnit":"центів","-KopSex":"M"},{"-CurrID":"840","-CurrName":"Долари США","-language":"ENG","-RubOneUnit":"dollar","-RubTwoUnit":"dollars","-RubFiveUnit":"dollars","-RubSex":"M","-RubShortUnit":"USD.","-KopOneUnit":"cent","-KopTwoUnit":"cents","-KopFiveUnit":"cents","-KopSex":"M"}],EUR:[{"-CurrID":"840","-CurrName":"Евро ЕС","-language":"RUS","-RubOneUnit":"евро","-RubTwoUnit":"евро","-RubFiveUnit":"евро","-RubSex":"M","-RubShortUnit":"евр.","-KopOneUnit":"цент","-KopTwoUnit":"цента","-KopFiveUnit":"центов","-KopSex":"M"},{"-CurrID":"840","-CurrName":"Евро ЕС","-language":"UKR","-RubOneUnit":"євро","-RubTwoUnit":"євро","-RubFiveUnit":"євро","-RubSex":"M","-RubShortUnit":"дол.","-KopOneUnit":"цент","-KopTwoUnit":"цента","-KopFiveUnit":"центів","-KopSex":"M"},{"-CurrID":"840","-CurrName":"Долари США","-language":"ENG","-RubOneUnit":"euro","-RubTwoUnit":"euros","-RubFiveUnit":"euros","-RubSex":"M","-RubShortUnit":"USD.","-KopOneUnit":"cent","-KopTwoUnit":"cents","-KopFiveUnit":"cents","-KopSex":"M"}],PER10:[{"-CurrID":"556","-CurrName":"Вiдсотки з десятими частинами","-language":"RUS","-RubOneUnit":"целая,","-RubTwoUnit":"целых,","-RubFiveUnit":"целых,","-RubSex":"F","-KopOneUnit":"десятая процента","-KopTwoUnit":"десятых процента","-KopFiveUnit":"десятых процента","-KopSex":"F"},{"-CurrID":"556","-CurrName":"Вiдсотки з десятими частинами","-language":"UKR","-RubOneUnit":"ціла,","-RubTwoUnit":"цілих,","-RubFiveUnit":"цілих,","-RubSex":"F","-KopOneUnit":"десята відсотка","-KopTwoUnit":"десятих відсотка","-KopFiveUnit":"десятих відсотка","-KopSex":"F"},{"-CurrID":"560","-CurrName":"Вiдсотки з десятими частинами","-language":"ENG","-RubOneUnit":",","-RubTwoUnit":"integers,","-RubFiveUnit":"integers,","-RubSex":"F","-KopOneUnit":"tenth of one percent","-KopTwoUnit":"tenth of one percent","-KopFiveUnit":"tenth of one percent","-KopSex":"F"}],PER100:[{"-CurrID":"557","-CurrName":"Вiдсотки з сотими частинами","-language":"RUS","-RubOneUnit":"целая,","-RubTwoUnit":"целых,","-RubFiveUnit":"целых,","-RubSex":"F","-KopOneUnit":"сотая процента","-KopTwoUnit":"сотых процента","-KopFiveUnit":"сотых процента","-KopSex":"F"},{"-CurrID":"557","-CurrName":"Вiдсотки з сотими частинами","-language":"UKR","-RubOneUnit":"ціла,","-RubTwoUnit":"цілих,","-RubFiveUnit":"цілих,","-RubSex":"F","-KopOneUnit":"сота відсотка","-KopTwoUnit":"сотих відсотка","-KopFiveUnit":"сотих відсотка","-KopSex":"F"},{"-CurrID":"561","-CurrName":"Вiдсотки з сотими частинами","-language":"ENG","-RubOneUnit":",","-RubTwoUnit":"integers,","-RubFiveUnit":"integers,","-RubSex":"F","-KopOneUnit":"hundred percent","-KopTwoUnit":"hundredth of percent","-KopFiveUnit":"hundredth of percent","-KopSex":"F"}],PER1000:[{"-CurrID":"558","-CurrName":"Вiдсотки з тисячними частинами","-language":"RUS","-RubOneUnit":"целая,","-RubTwoUnit":"целых,","-RubFiveUnit":"целых,","-RubSex":"F","-KopOneUnit":"тысячная процента","-KopTwoUnit":"тысячных процента","-KopFiveUnit":"тысячных процента","-KopSex":"F"},{"-CurrID":"558","-CurrName":"Вiдсотки з тисячними частинами","-language":"UKR","-RubOneUnit":"ціла,","-RubTwoUnit":"цілих,","-RubFiveUnit":"цілих,","-RubSex":"F","-KopOneUnit":"тисячна відсотка","-KopTwoUnit":"тисячних відсотка","-KopFiveUnit":"тисячних відсотка","-KopSex":"F"},{"-CurrID":"562","-CurrName":"Вiдсотки з тисячними частинами","-language":"ENG","-RubOneUnit":",","-RubTwoUnit":"integers,","-RubFiveUnit":"integers,","-RubSex":"F","-KopOneUnit":"thousandth of percent","-KopTwoUnit":"thousandths of percent","-KopFiveUnit":"thousandths of percent","-KopSex":"F"}],PER10000:[{"-CurrID":"559","-CurrName":"Вiдсотки з десяти тисячними частинами","-language":"RUS","-RubOneUnit":"целая,","-RubTwoUnit":"целых,","-RubFiveUnit":"целых,","-RubSex":"F","-KopOneUnit":"десятитысячная процента","-KopTwoUnit":"десятитысячные процента","-KopFiveUnit":"десятитысячных процента","-KopSex":"F"},{"-CurrID":"559","-CurrName":"Вiдсотки з десяти тисячними частинами","-language":"UKR","-RubOneUnit":"ціла,","-RubTwoUnit":"цілих,","-RubFiveUnit":"цілих,","-RubSex":"F","-KopOneUnit":"десятитисячна відсотка","-KopTwoUnit":"десятитисячних відсотка","-KopFiveUnit":"десятитисячних відсотка","-KopSex":"M"},{"-CurrID":"563","-CurrName":"Вiдсотки з десяти тисячними частинами","-language":"ENG","-RubOneUnit":",","-RubTwoUnit":"integers,","-RubFiveUnit":"integers,","-RubSex":"F","-KopOneUnit":"ten percent","-KopTwoUnit":"ten-percent","-KopFiveUnit":"ten-percent","-KopSex":"F"}]}},Currency=function(){function e(){}return e.UAH="UAH",e.RUR="RUR",e.USD="USD",e.EUR="EUR",e.PER10="PER10",e.PER100="PER100",e.PER1000="PER1000",e.PER10000="PER10000",e}(),Language=function(){function e(){}return e.RUS="RUS",e.UKR="UKR",e.ENG="ENG",e}(),Pennies=function(){function e(){}return e.NUMBER="NUMBER",e.TEXT="TEXT",e}(),StringBuilder=function(){function e(){this._buffer=[]}return e.prototype.append=function(e){return this._buffer[this._buffer.length]=e,this},e.prototype.insert=function(e,t){return this._buffer.splice(e,0,t),this},e.prototype.length=function(){return this.toString().length},e.prototype.deleteCharAt=function(e){var t=this.toString();return this._buffer=[],this.append(t.substring(0,e)),this},e.prototype.toString=function(){return this._buffer.join("")},e}(),MoneyToStr=function(){function U(e,t,n){this.currency=e,this.language=t,this.pennies=n;var i=t,u=currencyList.CurrencyList[i].item;for(var r in this.messages={},u){var o=u[r];o["-text"]&&(this.messages[o["-value"]]=o["-text"].split(","))}var a=currencyList.CurrencyList[e],U=null;for(var r in a)if(a[r]["-language"]==t){U=a[r];break}if(null==U)throw new Error("Currency not found "+e);this.rubOneUnit=U["-RubOneUnit"],this.rubTwoUnit=U["-RubTwoUnit"],this.rubFiveUnit=U["-RubFiveUnit"],this.kopOneUnit=U["-KopOneUnit"],this.kopTwoUnit=U["-KopTwoUnit"],this.kopFiveUnit=U["-KopFiveUnit"],this.rubSex=U["-RubSex"],this.kopSex=U["-KopSex"],this.rubShortUnit=U["-RubShortUnit"]}return U.NUM0=0,U.NUM1=1,U.NUM2=2,U.NUM3=3,U.NUM4=4,U.NUM5=5,U.NUM6=6,U.NUM7=7,U.NUM8=8,U.NUM9=9,U.NUM10=10,U.NUM11=11,U.NUM14=14,U.NUM100=100,U.NUM1000=1e3,U.NUM10000=1e4,U.INDEX_0=0,U.INDEX_1=1,U.INDEX_2=2,U.INDEX_3=3,U.percentToStr=function(e,t){if(null==e)throw new Error("amount is null");if(null==t)throw new Error("lang is null");var n=parseInt(e),i=0;return e==parseInt(e)?new U(Currency.PER10,t,Pennies.TEXT).convert(e,0):(e*U.NUM10).toFixed(4)==parseInt(e*U.NUM10)?(i=Math.round((e-n)*U.NUM10),new U(Currency.PER10,t,Pennies.TEXT).convert(n,i)):(e*U.NUM100).toFixed(4)==parseInt(e*U.NUM100)?(i=Math.round((e-n)*U.NUM100),new U(Currency.PER100,t,Pennies.TEXT).convert(n,i)):(e*U.NUM1000).toFixed(4)==parseInt(e*U.NUM1000)?(i=Math.round((e-n)*U.NUM1000),new U(Currency.PER1000,t,Pennies.TEXT).convert(n,i)):(i=Math.round((e-n)*U.NUM10000),new U(Currency.PER10000,t,Pennies.TEXT).convert(n,i))},U.prototype.convertValue=function(e){if(null==e)throw new Error("theMoney is null");var t=parseInt(e),n=Math.round((e-t)*U.NUM100);return this.currency==Currency.PER1000&&(n=Math.round((e-t)*U.NUM1000)),this.convert(t,n)},U.prototype.convert=function(e,t){if(null==e)throw new Error("theMoney is null");if(null==t)throw new Error("theKopeiki is null");var n=new StringBuilder,i=0,u=0,r=Math.abs(e);0==r&&n.append(this.messages[0][0]+" ");do{if(u=parseInt(r%U.NUM1000),n.insert(0,this.triad2Word(u,i,this.rubSex)),0==i){var o=parseInt(u%U.NUM100/U.NUM10),a=parseInt(u%U.NUM10);if(o==U.NUM1)n.append(this.rubFiveUnit);else switch(a){case U.NUM1:n.append(this.rubOneUnit);break;case U.NUM2:case U.NUM3:case U.NUM4:n.append(this.rubTwoUnit);break;default:n.append(this.rubFiveUnit)}}r=parseInt(r/U.NUM1000),i++}while(0<r);if(e<0&&n.insert(0,this.messages.minus[0]+" "),this.pennies==Pennies.TEXT?n.append(this.language==Language.ENG?" and ":" ").append(0==t?this.messages[0][0]+" ":this.triad2Word(Math.abs(t),0,this.kopSex)):n.append(" "+(Math.abs(t)<10?"0"+Math.abs(t):Math.abs(t))+" "),U.NUM11<=t&&t<=U.NUM14)n.append(this.kopFiveUnit);else switch(parseInt(t%U.NUM10)){case U.NUM1:n.append(this.kopOneUnit);break;case U.NUM2:case U.NUM3:case U.NUM4:n.append(this.kopTwoUnit);break;default:n.append(this.kopFiveUnit)}return n.toString().trim()},U.prototype.triad2Word=function(e,t,n){var i=new StringBuilder;if(0==e)return"";var u=this.check1(e,i);this.language==Language.ENG&&0<i.length()&&e%U.NUM10==0&&(i.deleteCharAt(i.length()-1),i.append(" "));var r=u;switch(u=parseInt(e%U.NUM10),this.check2(t,n,i,e,r),t){case U.NUM0:break;case U.NUM1:case U.NUM2:case U.NUM3:case U.NUM4:if(r==U.NUM1)i.append(this.messages["1000_10"][t-1]+" ");else switch(u){case U.NUM1:i.append(this.messages["1000_1"][t-1]+" ");break;case U.NUM2:case U.NUM3:case U.NUM4:i.append(this.messages["1000_234"][t-1]+" ");break;default:i.append(this.messages["1000_5"][t-1]+" ")}break;default:i.append("??? ")}return i.toString()},U.prototype.check2=function(e,t,n,i,u){var r=parseInt(i%U.NUM10);if(1==u)n.append(this.messages["10_19"][r]+" ");else switch(r){case U.NUM1:e==U.NUM1?n.append(this.messages[1][U.INDEX_0]+" "):e==U.NUM2||e==U.NUM3||e==U.NUM4?n.append(this.messages[1][U.INDEX_1]+" "):"M"==t?n.append(this.messages[1][U.INDEX_2]+" "):"F"==t&&n.append(this.messages[1][U.INDEX_3]+" ");break;case U.NUM2:e==U.NUM1?n.append(this.messages[2][U.INDEX_0]+" "):e==U.NUM2||e==U.NUM3||e==U.NUM4?n.append(this.messages[2][U.INDEX_1]+" "):"M"==t?n.append(this.messages[2][U.INDEX_2]+" "):"F"==t&&n.append(this.messages[2][U.INDEX_3]+" ");break;case U.NUM3:case U.NUM4:case U.NUM5:case U.NUM6:case U.NUM7:case U.NUM8:case U.NUM9:n.append(["","",""].concat(this.messages["3_9"])[r]+" ")}},U.prototype.check1=function(e,t){var n=parseInt(e/U.NUM100);return t.append([""].concat(this.messages["100_900"])[n]),n=parseInt(e%U.NUM100/U.NUM10),t.append(["",""].concat(this.messages["20_90"])[n]),n},U.prototype.getMessages=function(){return this.messages},U.prototype.getRubShortUnit=function(){return this.rubShortUnit},U}();