如何用javascript中的英文单词写一个整数? [英] How to write an integer in English words, in javascript?

查看:39
本文介绍了如何用javascript中的英文单词写一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我们有任何jquery方法接受一个int值并返回单词中的值吗?

hi do we have any jquery method that takes an int value and returns the value in words?

谢谢

推荐答案

此处的工作示例

这是一个从1到999的片段,您可以从 http://snippets.dzone.com/posts/show/3710

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;
}

这篇关于如何用javascript中的英文单词写一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆