【发布时间】:2014-10-15 08:29:28
【问题描述】:
我正在设计一个转换网站,我在其中对输入的数字进行计算,我需要我的输入或 textarea 来接收和解释以不同方式输入的数字
喜欢:
Entry = 3,000,000.1111
Interpreted value = 3000000.1111
或
Entry = 3000000.1111
Interpreted value = 3000000.1111
我想为欧洲十进制符号添加第二个输入 (或者如果可能有相同的输入两者都做)
Entry = 3.000.000,1111 (comma acts a decimal, decimal as separator)
Interpreted value = 3000000.1111
我想知道我怎么能做到这一点。我从我的一些研究中怀疑我可以使用正则表达式。 我还应该使用输入还是文本区域?我想将数字的大小限制为 40 个位置。
当使用逗号时,我当前使用的文本区域似乎无法识别逗号后的任何值。我意识到这是由于 parseFloat。所以我需要在解析之前使用 .replace() 删除逗号。但是在逗号是小数点的欧洲符号的情况下我该怎么办?我怀疑我应该使用正则表达式来确定一个数字是逗号十进制表示法还是标准小数点表示法,然后根据它概述适当的替换行为。任何想法如何编写正则表达式以仅通过分隔符和小数点识别 .0000000001 和 1,000,000,000,000,000 之间的数字?当条目不使用时怎么办?以 12000 为例。对此的任何帮助将不胜感激。使用 HTML5 和 Javascript。我没有使用表格,对此我很陌生。这是我的第一个网页,所以请耐心解答我的问题。
我在想这个:
input = //value from textarea as a string
if(/REGEX which determines that the structure of the number is N,NNN.NN/.test(input)){
input = input.replace(/\,/,""); //replace the commas with nothing
}
else if(/REGEX which determine that structure of the number is N.NNN,NN/.test(input){
input = input.replace(/\./,""); //replace the decimal point separators with nothing
input = input.replace(/\,/,".");//replace the comma decimal with a point decimal
}
else{
//input unchanged assuming is NNNN without decimal
}
number = parseFloat(input);
我想让他们有可能输入大数字并使用小数点后 1 到 10 位以下的数字。感谢那些做出贡献的人。 最佳,RP
【问题讨论】:
标签: javascript html regex input textarea