Javascript将数据转成英文书写格式
2023-12-03 08:04:18
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
最新文章
- 解决问题:无法对 System程序集 添加Fakes程序集
- 如何防止JAVA反射对单例类的攻击?
- 冲刺阶段 day12
- hibernate----(Hql)查询
- AWS S3 API实现文件上传下载
- codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
- 主框架搭建demo
- [spring]启动时报错:NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
- lua的split函数
- 【hbase】使用thrift with python 访问HBase
- php正则提取img所有属性值
- IIS 发布程序,无法输出EXCEL 问题处理
- vue-router 路由跳转:和name配对的是params,和path配对的是query
- Android必学之数据适配器BaseAdapter
- 【API】检查进程是否存在 - CreateToolhelp32Snapshot
- mybatis四大接口之 ResultSetHandler
- HTTP 请求头中的 X-Forwarded-For,X-Real-IP
- SharePoint 2016 安装 Cumulative Update for Service Bus 1.0 (KB2799752)报错
- C/C++中的实参和形参,重点以及盲点,自己以前未知的
- JZOJ.5335【NOIP2017模拟8.24】早苗