How to write an integer in English words, in Javascript? [closed]

只谈情不闲聊 提交于 2019-12-23 04:58:11

问题


Do we have any jQuery method that takes an int value and returns the value in words?


回答1:


Working example here

Here's a snippet that works from 1 to 999 that you might be able to modify to go higher, from http://snippets.dzone.com/posts/show/3710:

var units = new Array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
var tens = new Array ("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");

function num(it) {
    var theword = "";
    var started;
    if (it>999) return "Lots";
    if (it==0) return units[0];
    for (var i = 9; i >= 1; i--){
        if (it>=i*100) {
            theword += units[i];
            started = 1;
            theword += " hundred";
            if (it!=i*100) theword += " and ";
            it -= i*100;
            i=0;
        }
    };

    for (var i = 9; i >= 2; i--){
        if (it>=i*10) {
            theword += (started?tens[i-2].toLowerCase():tens[i-2]);
            started = 1;
            if (it!=i*10) theword += "-";
            it -= i*10;
            i=0
        }
    };

    for (var i=1; i < 20; i++) {
        if (it==i) {
            theword += (started?units[i].toLowerCase():units[i]);
        }
    };
    return theword;
}



回答2:


No, but you can make your own.

Something like this may be a good place to start, although it has some bugs:

http://abhisanoujam.blogspot.com/2009/07/having-fun-with-jquery-numbers-to-words.html

Or this plain JavaScript version:

http://javascript.about.com/library/bltoword.htm




回答3:


No, there's nothing built into either JavaScript or jQuery that provides that feature.




回答4:


Try jquery.num2words.js

Source: https://github.com/faizalmansor/num2words



来源:https://stackoverflow.com/questions/5904851/how-to-write-an-integer-in-english-words-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!