Javascript - How to convert string '0' to number 0 - Stack Overflow

I'm trying to do error handling on 2 input values. I'm using regex to confirm that the input

I'm trying to do error handling on 2 input values. I'm using regex to confirm that the input is always a number. The issue I'm having is that I don't want my error handling to kick in if the user literally inputs 0. Right now I'm using:

number = parseInt(iningValue) || ""

to set my variable. The issue is that this turns '0' into ""

Its fine if an empty value bees an empty string because I am disabling my error checking when the lengths are equal to 0, but I need to properly turn '0' into a number 0. Anyone have any ideas?

Additionally, I'd also like to turn '000' (and so forth) into a number 0

I'm trying to do error handling on 2 input values. I'm using regex to confirm that the input is always a number. The issue I'm having is that I don't want my error handling to kick in if the user literally inputs 0. Right now I'm using:

number = parseInt(iningValue) || ""

to set my variable. The issue is that this turns '0' into ""

Its fine if an empty value bees an empty string because I am disabling my error checking when the lengths are equal to 0, but I need to properly turn '0' into a number 0. Anyone have any ideas?

Additionally, I'd also like to turn '000' (and so forth) into a number 0

Share Improve this question edited Feb 8, 2022 at 10:20 Super Kai - Kazuya Ito 1 asked Apr 1, 2020 at 23:03 Joe SchmoeJoe Schmoe 313 silver badges6 bronze badges 1
  • Does this answer your question? Convert a string to an integer? – 0stone0 Commented Apr 1, 2020 at 23:11
Add a ment  | 

4 Answers 4

Reset to default 4

You can turn '0' or '000' into a number by just doing:

parseInt('0');   // 0
parseInt('000'); // 0

The reason your code is not working is that javascript treats 0 as a falsly value, so when you do this:

const number = parseInt('0') || ""

the expression parseInt('0') will return 0 which is falsy. As a result, the || "" will be executed which will set number to "". You'll need to separate your parseInt and your default assignment to achieve what you want.

Use "Number()":

console.log(Number('0'));
console.log(Number('000'));

console.log(typeof(Number('0')));
console.log(typeof(Number('000')));

Or put "+" before '0' and '000':

console.log(+'0');
console.log(+'000');

console.log(typeof(+'0'));
console.log(typeof(+'000'));

Or put "* 1" before or after '0' and '000':

console.log('0' * 1);
console.log('000' * 1);

console.log(typeof('0' * 1));
console.log(typeof('000' * 1));

You can use parseInt(iningValue) to get the int value. For paring you can use === for equal value and equal type means (iningValue === 0) will be true in case of iningValue = 0.

You can try typeof to distinguish what type of variable you are receiving

typeof true === 'boolean'

typeof null === 'object'

typeof 62 === 'number'

typeof 'Hello World' === 'string'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744298904a4567400.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信