十进制数与逗号和点Javascript之间的差异

时间:2021-10-29 22:32:12

I should validate an input form testing if value is >= 0.

如果值> = 0,我应该验证输入表单测试。

I get the value with jQuery and validate with a simple if statements. like this:

我使用jQuery获取值并使用简单的if语句进行验证。喜欢这个:

var value = $('#'+this.id).val();
    console.log("value is " + value );
    if(value >=0 ){
      console.log("true");
    }else{
      console.log("false");
    }

The problem raise when user inserts decimal number with comma or dot. If user insert 1.234 the result is true instead if inserts 1,234 the result is false

用户使用逗号或点插入十进制数时问题会出现。如果用户插入1.234,则结果为true,如果插入1,234,则结果为false

What is their difference?

他们有什么区别?

I make a simple Fiddle for explain better the case.

我做了一个简单的小提琴,以便更好地解释这个案子。

http://jsfiddle.net/zzm8zno5/4/

http://jsfiddle.net/zzm8zno5/4/

I also verified the type of value; I tried to cast as number with Number(value) and nothing change

我还验证了价值的类型;我尝试使用Number(值)转换为数字而没有任何变化

Sorry for my question, maybe is trivial but i don't understand why this happen

对不起我的问题,也许是微不足道但我不明白为什么会发生这种情况

3 个解决方案

#1


3  

Javascript does not recognise the comma as the decimal separator. You have to use a dot, ..

Javascript无法将逗号识别为小数点分隔符。你必须使用一个点,..

To do this you can use replace() to change the comma in the value. Also you need to use parseFloat() to compare the value, otherwise you're comparing a number (0) to a string ('1.234'). Try this:

为此,您可以使用replace()更改值中的逗号。您还需要使用parseFloat()来比较值,否则您将数字(0)与字符串('1.234')进行比较。尝试这个:

var value = parseFloat($(this).val().replace(',', '.'));

Updated fiddle

更新小提琴

Finally, note the use of just this in the jQuery object. Since you already have a reference to the element you don't need to build another string selector. If you prefer you could shorten that further to just this.value

最后,请注意在jQuery对象中使用这个。由于您已经拥有对该元素的引用,因此您不需要构建另一个字符串选择器。如果你愿意,你可以进一步缩短到这个值

Update

更新

Assuming that 1,234 is intended to mean 1234 and is not used as a decimal separator then you need to instead remove the , without replacement:

假设1,234意图表示1234并且不用作小数分隔符,那么您需要删除,而不是替换:

var value = parseFloat($(this).val().replace(',', ''));

#2


1  

I agree with @Rory about removing the comma (',') except that you need to replace it with dot ('.'). I believe what you want to happen is to get only the numbers, not replacing comma with dots.

我同意@Rory关于删除逗号(',')除了你需要用点('。')替换它。我相信你想要发生的只是得到数字,而不是用点代替逗号。

1,234 = 1234 and not 1.234

use only this:

仅使用此:

var value = parseFloat($(this).val().replace(',', ''));

Here's a JSFiddel update from @rory's answer

这是来自@ rory答案的JSFiddel更新

#3


0  

Javascript only knows 64bit double precision floating point variables. Your comma is splitting the double to two decimals.

Javascript只知道64位双精度浮点变量。你的逗号将双精度值分成两位小数。

You need to stringreplace the input (, to .)

你需要stringreplace输入(,to。)

#1


3  

Javascript does not recognise the comma as the decimal separator. You have to use a dot, ..

Javascript无法将逗号识别为小数点分隔符。你必须使用一个点,..

To do this you can use replace() to change the comma in the value. Also you need to use parseFloat() to compare the value, otherwise you're comparing a number (0) to a string ('1.234'). Try this:

为此,您可以使用replace()更改值中的逗号。您还需要使用parseFloat()来比较值,否则您将数字(0)与字符串('1.234')进行比较。尝试这个:

var value = parseFloat($(this).val().replace(',', '.'));

Updated fiddle

更新小提琴

Finally, note the use of just this in the jQuery object. Since you already have a reference to the element you don't need to build another string selector. If you prefer you could shorten that further to just this.value

最后,请注意在jQuery对象中使用这个。由于您已经拥有对该元素的引用,因此您不需要构建另一个字符串选择器。如果你愿意,你可以进一步缩短到这个值

Update

更新

Assuming that 1,234 is intended to mean 1234 and is not used as a decimal separator then you need to instead remove the , without replacement:

假设1,234意图表示1234并且不用作小数分隔符,那么您需要删除,而不是替换:

var value = parseFloat($(this).val().replace(',', ''));

#2


1  

I agree with @Rory about removing the comma (',') except that you need to replace it with dot ('.'). I believe what you want to happen is to get only the numbers, not replacing comma with dots.

我同意@Rory关于删除逗号(',')除了你需要用点('。')替换它。我相信你想要发生的只是得到数字,而不是用点代替逗号。

1,234 = 1234 and not 1.234

use only this:

仅使用此:

var value = parseFloat($(this).val().replace(',', ''));

Here's a JSFiddel update from @rory's answer

这是来自@ rory答案的JSFiddel更新

#3


0  

Javascript only knows 64bit double precision floating point variables. Your comma is splitting the double to two decimals.

Javascript只知道64位双精度浮点变量。你的逗号将双精度值分成两位小数。

You need to stringreplace the input (, to .)

你需要stringreplace输入(,to。)