JavaScript 数字到单词
我正在尝试将数字转换为英文单词,例如 1234 将变为:“一千二百三十四”。
我的策略是这样的:
将数字分成三位,然后将它们从右到左放入数组 (
finlOutPut
)。将每组(
finlOutPut
数组中的每个单元格)的三位数字转换为一个单词(这是triConvert
函数的作用)。如果所有三位数字均为零,则将它们转换为"dontAddBigSuffix"
从右到左,添加千、百万、十亿等。如果
finlOutPut
单元格等于"dontAddBigSufix"
(因为它只是零),则不要添加该单词并将单元格设置为" "
(什么也没有)。
它似乎工作得很好,但我在处理诸如 190000009 之类的数字时遇到了一些问题,转换为:“一亿九千万”。当有几个零时,它会以某种方式“忘记”最后的数字。
我做错了什么?错误在哪里?为什么它不能完美工作?
function update(){
var bigNumArry = new Array('', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion');
var output = '';
var numString = document.getElementById('number').value;
var finlOutPut = new Array();
if (numString == '0') {
document.getElementById('container').innerHTML = 'Zero';
return;
}
if (numString == 0) {
document.getElementById('container').innerHTML = 'messeg tell to enter numbers';
return;
}
var i = numString.length;
i = i - 1;
//cut the number to grups of three digits and add them to the Arry
while (numString.length > 3) {
var triDig = new Array(3);
triDig[2] = numString.charAt(numString.length - 1);
triDig[1] = numString.charAt(numString.length - 2);
triDig[0] = numString.charAt(numString.length - 3);
var varToAdd = triDig[0] + triDig[1] + triDig[2];
finlOutPut.push(varToAdd);
i--;
numString = numString.substring(0, numString.length - 3);
}
finlOutPut.push(numString);
finlOutPut.reverse();
//conver each grup of three digits to english word
//if all digits are zero the triConvert
//function return the string "dontAddBigSufix"
for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}
var bigScalCntr = 0; //this int mark the million billion trillion... Arry
for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}
//convert The output Arry to , more printable string
for(n = 0; n<finlOutPut.length; n++){
output +=finlOutPut[n];
}
document.getElementById('container').innerHTML = output;//print the output
}
//simple function to convert from numbers to words from 1 to 999
function triConvert(num){
var ones = new Array('', ' 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');
var hundred = ' hundred';
var output = '';
var numString = num.toString();
if (num == 0) {
return 'dontAddBigSufix';
}
//the case of 10, 11, 12 ,13, .... 19
if (num < 20) {
output = ones[num];
return output;
}
//100 and more
if (numString.length == 3) {
output = ones[parseInt(numString.charAt(0))] + hundred;
output += tens[parseInt(numString.charAt(1))];
output += ones[parseInt(numString.charAt(2))];
return output;
}
output += tens[parseInt(numString.charAt(0))];
output += ones[parseInt(numString.charAt(1))];
return output;
}
<input type="text"
id="number"
size="70"
onkeyup="update();"
/*this code prevent non numeric letters*/
onkeydown="return (event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )"/>
<br/>
<div id="container">Here The Numbers Printed</div>
I'm trying to convert numbers into english words, for example 1234 would become: "one thousand two hundred thirty four".
My Tactic goes like this:
Separate the digits to three and put them on Array (
finlOutPut
), from right to left.Convert each group (each cell in the
finlOutPut
array) of three digits to a word (this what thetriConvert
function does). If all the three digits are zero convert them to"dontAddBigSuffix"
From Right to left, add thousand, million, billion, etc. If the
finlOutPut
cell equals"dontAddBigSufix"
(because it was only zeroes), don't add the word and set the cell to" "
(nothing).
It seems to work pretty well, but I've got some problems with numbers like 190000009, converted to: "one hundred ninety million". Somehow it "forgets" the last numbers when there are a few zeros.
What did I do wrong? Where is the bug? Why does it not work perfectly?
function update(){
var bigNumArry = new Array('', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion');
var output = '';
var numString = document.getElementById('number').value;
var finlOutPut = new Array();
if (numString == '0') {
document.getElementById('container').innerHTML = 'Zero';
return;
}
if (numString == 0) {
document.getElementById('container').innerHTML = 'messeg tell to enter numbers';
return;
}
var i = numString.length;
i = i - 1;
//cut the number to grups of three digits and add them to the Arry
while (numString.length > 3) {
var triDig = new Array(3);
triDig[2] = numString.charAt(numString.length - 1);
triDig[1] = numString.charAt(numString.length - 2);
triDig[0] = numString.charAt(numString.length - 3);
var varToAdd = triDig[0] + triDig[1] + triDig[2];
finlOutPut.push(varToAdd);
i--;
numString = numString.substring(0, numString.length - 3);
}
finlOutPut.push(numString);
finlOutPut.reverse();
//conver each grup of three digits to english word
//if all digits are zero the triConvert
//function return the string "dontAddBigSufix"
for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}
var bigScalCntr = 0; //this int mark the million billion trillion... Arry
for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}
//convert The output Arry to , more printable string
for(n = 0; n<finlOutPut.length; n++){
output +=finlOutPut[n];
}
document.getElementById('container').innerHTML = output;//print the output
}
//simple function to convert from numbers to words from 1 to 999
function triConvert(num){
var ones = new Array('', ' 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');
var hundred = ' hundred';
var output = '';
var numString = num.toString();
if (num == 0) {
return 'dontAddBigSufix';
}
//the case of 10, 11, 12 ,13, .... 19
if (num < 20) {
output = ones[num];
return output;
}
//100 and more
if (numString.length == 3) {
output = ones[parseInt(numString.charAt(0))] + hundred;
output += tens[parseInt(numString.charAt(1))];
output += ones[parseInt(numString.charAt(2))];
return output;
}
output += tens[parseInt(numString.charAt(0))];
output += ones[parseInt(numString.charAt(1))];
return output;
}
<input type="text"
id="number"
size="70"
onkeyup="update();"
/*this code prevent non numeric letters*/
onkeydown="return (event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )"/>
<br/>
<div id="container">Here The Numbers Printed</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
你的问题已经解决了,但我发布了另一种方法仅供参考。
该代码是为了在 Node.js 上进行测试而编写的,但在浏览器中调用时这些函数应该可以正常工作。此外,这仅处理范围 [0,1000000],但可以轻松适应更大的范围。
Your problem is already solved but I am posting another way of doing it just for reference.
The code was written to be tested on node.js, but the functions should work fine when called within the browser. Also, this only handles the range [0,1000000], but can be easily adapted for bigger ranges.
当存在前导零数字时,JavaScript 会将 3 个数字组成的组解析为八进制数。当三位数字全为零时,无论基数是八进制还是十进制,结果都是一样的。
但是当你给 JavaScript '009'(或'008')时,这是一个无效的八进制数,所以你会得到零。
如果您浏览了从 190,000,001 到 190,000,010 的整组数字,您会看到 JavaScript 跳过“...,008”和“...,009”,但为“...,010”发出“8”。这就是“尤里卡!”片刻。
Change:
to
Code 还在每个非零组后面添加逗号,所以我使用它并找到了添加逗号的正确位置。
旧:
新:
JavaScript is parsing the group of 3 numbers as an octal number when there's a leading zero digit. When the group of three digits is all zeros, the result is the same whether the base is octal or decimal.
But when you give JavaScript '009' (or '008'), that's an invalid octal number, so you get zero back.
If you had gone through the whole set of numbers from 190,000,001 to 190,000,010 you'd hav seen JavaScript skip '...,008' and '...,009' but emit 'eight' for '...,010'. That's the 'Eureka!' moment.
Change:
to
Code also kept on adding commas after every non-zero group, so I played with it and found the right spot to add the comma.
Old:
New:
在花了一些时间谷歌搜索和玩其他代码之后,我做了一个快速修复,可重用函数适用于高达 99,99,99,999 的数字。 use :
number2text(1234.56);
将返回1000234 RUPEE AND 56 PAISE ONLY
。享受 !After spending some time in googling and playing with others code i made a quick fix and reusable function works well for numbers upto 99,99,99,999. use :
number2text(1234.56);
will returnONE THOUSAND TWO HUNDRED AND THIRTY-FOUR RUPEE AND FIFTY-SIX PAISE ONLY
. enjoy !@jasonhao 的印度货币答案的更新版本
Updated version of @jasonhao 's answer for Indian currency
在这里,我写了一个替代解决方案:
1)包含字符串常量的对象:
2)实际代码:
现场演示: http://jsfiddle.net/j5kdG/
Here, I wrote an alternative solution:
1) The object containing the string constants:
2) The actual code:
Live demo: http://jsfiddle.net/j5kdG/
有en_US和cs_CZ的JS库。
您可以单独使用它,也可以将其作为 Node 模块使用。
There are JS library for en_US and cs_CZ.
You can use it standalone or as Node module.
2022 年 2 月更新
下面包含一个简单而简短的 Javascript 函数,用于将数字(整数)转换为英语(美国系统)中的单词,并使用带有字符串的
forEach()
循环来给出正确的结果三元组数组方法。首先使用以下方法将数字转换为字符串三元组数组:
然后将字符串数组轻松解码为数字。
例如,数字 1234567 被转换为字符串三元组数组:
然后通过
forEach()
循环从左到右扫描每个元素。这是一个比我 2 年前提出的使用
for
循环更好的解决方案。如果您愿意,可以将比例数组增加到“四亿”以上。
如果输入的数字太大,则将其作为“字符串”传递。
还包括测试用例。
Update Feb 2022
A simple and short Javascript function to do the conversion of numbers (integers) to words in English (US System) and give the correct results is included below using a
forEach()
loop with the String Triplets Array method.The number is first converted into a string triplet array using:
The string array is then easily decoded into numbers.
For example, the number 1234567 is converted into an array of string triplets:
Each element is then scanned from left to right by the
forEach()
loop.This is a far better solution than using a
for
loop that I had proposed 2 years ago.You can increase the scale array above the 'Quadrillion' if you prefer.
If the input number is too large, then pass it as a "string".
Test cases are also included.
带有紧凑对象的版本 - 适用于从 0 到 999 的数字。
A version with a compact object - for numbers from zero to 999.
试试这个,将数字转换为单词
Try this,convert number to words
这是一个简单的 ES6+ 数字到单词的函数。您可以简单地添加“illions”数组来扩展数字。美式英语版本。
(结尾前没有“和”)
This is a simple ES6+ number to words function. You can simply add 'illions' array to extend digits. American English version.
(no 'and' before the end)
这是一个解决方案,可以处理任何适合字符串的整数值。我已将数字比例定义为“十亿”,因此该解决方案应精确到 999 十亿。之后你会得到诸如“一千十亿”之类的东西。
JavaScript 数字在“999999999999999”左右开始失败,因此转换函数仅适用于数字字符串。
示例:
代码:
Here's a solution that will handle any integer value that fit's in a string. I've defined number scales up to "decillion", so this solution should be accurate up to 999 decillion. After which you get things like "one thousand decillion" and so on.
JavaScript numbers start to fail around "999999999999999" so the convert function works with strings of numbers only.
Examples:
Code:
我修改了 Šime Vidas 的帖子 - http://jsfiddle.net/j5kdG/
在适当的位置包含美元、美分、逗号和“和”。
如果需要“零美分”,或者如果为 0,则不提及美分,则有一个可选的结尾。
这个函数结构让我有点头疼,但我学到了很多。谢谢西梅。
有人可能会找到更好的方法来处理这个问题。
代码:
I've modified the posting from Šime Vidas - http://jsfiddle.net/j5kdG/
To include dollars, cents, commas and "and" in the appropriate places.
There's an optional ending if it requires "zero cents" or no mention of cents if 0.
This function structure did my head in a bit but I learned heaps. Thanks Sime.
Someone might find a better way of processing this.
Code:
这是法语的解决方案
这是 @gandil 回复的一个分支,
我希望它会有所帮助
this is the solution for french language
it's a fork for @gandil response
i hope it will help
INDONESIAN DEVELOPER 的一个小代码片段,用于将数值转换为口头拼写(手动最小化,仅 576 字节,需要 ES6+)...
A little code snippet for INDONESIAN DEVELOPER to translate numeric value into verbal spelling (minimized by hand, only 576 bytes, requires ES6+)...
我想指出的是,对于 x11-x19 之间的值(其中 x >= 1),原始逻辑会失败。例如,118 返回“一百八”。这是因为这些数字是通过 triConvert() 中的以下代码处理的:
这里,代表十位数的字符用于索引
tens[]
数组,该数组的索引 [1 处有一个空字符串],因此 118 实际上变成了 108。最好先处理数百个(如果有的话),然后通过相同的逻辑运行个数和十个。而不是:
我建议:
在我看来,建议的代码既更短又更清晰,但我可能有偏见;-)
I would like to point out that the original logic fails for values between x11-x19, where x >= 1. For example, 118 returns "one hundred eight". This is because these numbers are processed by the following code in triConvert():
here, the character representing the tens digit is used to index the
tens[]
array, which has an empty string at index [1], so 118 become 108 in effect.It might be better to deal with the hundreds (if any first), then run the ones and tens through the same logic. Instead of:
I would suggest:
It seems to me that the suggested code is both shorter and clearer, but I might be biased ;-)
来源:http://javascript.about.com/library/bltoword.htm
我发现的最小脚本:
享受吧!
Source: http://javascript.about.com/library/bltoword.htm
Smallest script that I found:
Enjoy!
我尝试了穆罕默德的解决方案,但遇到了一些问题并且想要使用小数,所以我做了一些更改并转换为咖啡脚本和角度。请记住,js和coffeescript不是我的强项,所以要小心使用。
$scope.is_numeric = (mixed_var) ->;
# console.log“混合变量是:”+mixed_var
(typeof mix_var == 'number' 或 typeof mix_var == 'string') and mix_var != '' and !isNaN(mixed_var)
I tried Muhammad's solution, but had some issues and wanted to use decimals so I made some changes and converted to coffeescript and angular. Please bear in mind that js and coffeescript are not my strong suits, so use with care.
$scope.is_numeric = (mixed_var) ->
# console.log "mixed var is: " + mixed_var
(typeof mixed_var == 'number' or typeof mixed_var == 'string') and mixed_var != '' and !isNaN(mixed_var)
这是我的另一个版本,其中包含一些单元测试。
请勿将其与大于
Number.MAX_SAFE_INTEGER
的数字一起使用。Here is another version from me with some unit tests.
Don't use it with numbers larger than
Number.MAX_SAFE_INTEGER
.我想,我有更简单、更容易理解的解决方案;它通过对数字进行切片,最多可达 990 亿卢比。
请查看我的代码笔
I think, I have solution that's simpler and easier to understand; it goes by slicing the number, it works up to 99 lakhs crore.
Please check out my code pen
如果有人想用西班牙语(en español)执行此操作,这是我基于 Hardik 的
结果的代码:
If anybody ever wants to do this but in Spanish (en español), here's my code based on Hardik's
Result:
我在这里提供了我的解决方案,使用单循环字符串三元组 (SLST) 方法将数字转换为等效的英语单词,该方法我已在 Code Review 中发布并通过插图进行了详细解释。
使用JavaScript 中的单循环字符串三元组
这个概念简单且易于编码,而且非常高效和快速。与本文中描述的其他替代方法相比,该代码非常短。
该概念还允许转换非常不同的大数字,因为它不依赖于使用语言的数字/数学函数,因此避免了任何限制。
如果需要,可以通过在“Decillion”之后添加更多比例名称来增加比例数组。
下面提供了一个示例测试函数,以生成 1 到 1099 之间的数字为例。
提供完整的精美版本,可以在刻度和数字之间添加单词“and”和逗号,以与英国和美国的数字拼写方式保持一致。
希望这有用。
I am providing here my solution for converting numbers to the equivalent English words using the Single Loop String Triplets (SLST) Method which I have published and explained in detail here at Code Review with illustration graphics.
Simple Number to Words using a Single Loop String Triplets in JavaScript
The concept is simple and easily coded and is also very efficient and fast. The code is very short compared with other alternative methods described in this article.
The concept also permits the conversion of very vary large numbers as it does not rely on using the numeric/math function of the language and therefore avoids any limitations.
The Scale Array may be increased by adding more scale names if required after "Decillion".
A sample test function is provided below to generate numbers from 1 to 1099 as an example.
A full fancy version is available that caters for adding the word "and" and commas between the scales and numbers to align with the UK and US ways of spelling the numbers.
Hope this is useful.
我知道这个问题已经得到解答,但在看到这个问题之前我一直在研究这个问题。没有在其他答案中发现一些小错误。支持高达vigintillion。
I know this has been answered but I had been working on this before seeing the question. Doesn't have some small errors found in other answers. Supports up to vigintillion.
https://stackoverflow.com/a/69243038/8784402
基于 ES2022 + 最快的 TypeScript 版本
在 Typescript Playground 上测试
印地语版本
印度语 英语版本
国际版本
货币转换示例
https://stackoverflow.com/a/69243038/8784402
TypeScript Version Based on ES2022 + Fastest
Test on Typescript Playground
Hindi Version
Indian English Version
International Version
Example to convert currency
这就是我解决这个问题并负责英语和法语等多种语言的方法
this is how i solved this problem and take in charge multiple language as english and french
我认为这会对你有帮助
I think this will help you