Javascript将数据转成英文书写格式
2024-08-10 00:05:33
var aTens = [ "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"];
var aOnes = [ "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE",
"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
"NINETEEN" ]; function ConvertToHundreds(num)
{
var cNum, nNum;
var cWords = ""; num %= 1000;
if (num > 99) {
/* Hundreds. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aOnes[nNum] + " HUNDRED";
num %= 100;
if (num > 0)
cWords += " AND "
} if (num > 19) {
/* Tens. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aTens[nNum - 2];
num %= 10;
if (num > 0)
cWords += " ";
} if (num > 0) {
/* Ones and teens. */
nNum = Math.floor(num);
cWords += aOnes[nNum];
} return cWords;
} function ConvertToWords(num)
{
var aUnits = [ "THOUSAND", "MILLION", "BILLION", "TRILLION", "QUADRILLION" ];
var cWords = (num >= 1 && num < 2) ? "" : "";
var nLeft = Math.floor(num);
for (var i = 0; nLeft > 0; i++) {
if (nLeft % 1000 > 0) {
if (i != 0)
cWords = ConvertToHundreds(nLeft) + " " + aUnits[i - 1] + " " + cWords;
else
cWords = ConvertToHundreds(nLeft) + " " + cWords;
}
nLeft = Math.floor(nLeft / 1000);
}
num = Math.round(num * 100) % 100;
if (num > 0)
cWords += ConvertToHundreds(num) + " CENTS ";
// else
// cWords += "Zero Cents"; return cWords+'ONLY';
} 调用:
ConvertToWords(123,450,000.66) -->ONE HUNDRED AND TWENTY THREE MILLION FOUR HUNDRED AND FIFTY THOUSAND SIXTY SIX CENTS ONLY
最新文章
- ie6并不是不支持!important
- Jquer的三种初始化方式
- 【英语】Bingo口语笔记(49) - 春节请客吃饭的表达
- Jquery实现页面上所有的checkbox只能选中一个
- POJ 3181 Dollar Dayz(高精度 动态规划)
- 来晚了--SALTSTACK要弄起
- mousewheel 与 DOMMouseScroll
- Coursera台大机器学习基础课程1
- EditText光标居上
- Eclipse EE遇到问题记录
- Python3 系列之 基础语法篇
- Windows10 Build 18298 桌面显示计算机(此电脑)
- 大学jsp实验七--JavaBean在JSP中的应用
- 上传程序Dictionary 字典 哈希--多读一写锁ReaderWriterLock
- [转]使用tee记录mysql client 所有的操作,十个节省时间的mysql命令
- Py中re.sub学习【转载】
- GE与POST方法区别
- String类型转json 转JSONObject 转 JSONArray 以及 遍历
- Linux每天一个命令:iperf
- dpkg: warning: files list file for package `*****&#39; missing, assuming package has no files currently installed解决办法