为什么我的textarea(HTML)值显示为12,000,000.11,但是parseFloat之后的值只有12? [英] Why does my textarea (HTML) value show 12,000,000.11 but after parseFloat the value is only 12?

查看:112
本文介绍了为什么我的textarea(HTML)值显示为12,000,000.11,但是parseFloat之后的值只有12?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个具有数值的转换网站:

I am attempting to develop a conversion website that takes a numeric value:

 1,200.12
 or
 1.200,12
 or
 1200.12
 or
 1200,12
 and have them all interpreted as 1200.12 by parseFloat.

 I would also like decimals to be able to be interpreted.
 0.123
 or
 0,123     
 as 0.123

通过文本区域,然后parseFloat浮点数字以执行计算. 这些是我得到的结果:

through a textarea and then parseFloat the number in order to perform calculations. These are the results I am getting:

textarea input = 12,000.12
value after parseFloat = 12

parseFloat不能识别数字的格式吗? 我得到相同的结果:

Does parseFloat not recognize the formatting of the numbers? i get the same results with:

textarea input: 12.000,12
value after parseFloat = 12

如何解决此问题?似乎我需要去除逗号,因为parseFloat不会读取超出它们的内容,并且使用欧洲符号会去除小数点并将逗号更改为十进制,以使parseFloat可以正确读取输入.关于如何解决这个问题的任何想法?我的猜测是,我需要将输入的字符串标识为欧洲或美国的十进制表示法,然后执行所需的操作来为parseFloat准备字符串.我将如何实现这一目标?感谢所有贡献.使用HTML5和Javascript.这是我的第一个网站,所以请放心.

How do I solve this problem? It would seem I need to strip out the commas since parseFloat doesn't read beyond them and with european notation strip the decimals and change the comma to a decimal for parseFloat to read the input correctly. Any ideas on how to solve this? My guess is I would need to identify the string input as either european or american decimal notation and then perform the required actions to prepare the string for parseFloat. How would I go about achieving that? All contributions are appreciated. Using HTML5 and Javascript. This is my first website so please go easy on me.

最好, RP

致所有贡献者...谢谢!到目前为止,所有输入都很不错.我认为我们无法使用单个replace语句正确剥离欧洲和美国符号,因此我认为我应该以某种方式使用REGEX来确定符号,然后拆分为if else语句来执行单独的替换功能在每个单独的符号上.

To all contributors...Thank you! So far all the input has been sweet. I don't think we are going to be able to use a single replace statement to correctly strip both european and american notation so I think I should use REGEX somehow to determine the notation and then split into an if else statement to perform separate replace functions on each individual notation.

var input, trim;
input = "1.234,56"   //string from textarea on page
if(/REGEX that determines American Notation/.test(input){ 
   trim =  input.replace(/\,/,"");//removes commas and leaves decimal point);
}
else(/REGEX that determine European Notation/.test(input)/){ //would qualify input here
   rep = input.replace(/\./,"");//removes all decimal points);
   trim = rep.replace(/\,/,"."//changes the remaining comma to a decimal);
}
//now either notation should be in the appropriate form to parse
number = parseFloat(trim);

使用REGEX可以吗?请参阅我的其他问题. 正则表达式-创建可正确解释数字的输入/文本区域

Is this possible using REGEX? Please see my other question. Regex - creating an input/textarea that correctly interprets numbers

推荐答案

这里是一种使用正则表达式消除所有逗号和所有句点(最后一个除外)的解决方案.

Here is a solution that uses a regular expression to eliminate all commas and all periods, except the last one.

var number = "1,234.567.890";
var replaced = number.replace(/,|\.(?=.*\.)/g, "");
var result = parseFloat(replaced);
// result === 1234567.89

或者,您可以使用此方法,它以相同的方式处理逗号和句点,并且忽略除最后一个以外的所有逗号和句点.

Alternatively, you can use this, which treats commas and periods identically, and ignores them all except for the last one.

var number = "12.345,67";
var replaced = number.replace(/[.,](?=.*[.,])/g, "").replace(",", ".");
var result = parseFloat(replaced);
// result === 12345.67

这篇关于为什么我的textarea(HTML)值显示为12,000,000.11,但是parseFloat之后的值只有12?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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