How do I retain leading zeroes in an IntegerNumber in JavaScript? - Stack Overflow

I am trying to retain leading zeroes in an integernumber within JavaScript.In this example I have an

I am trying to retain leading zeroes in an integer/number within JavaScript.

In this example I have an "ItemName" key which has either a string or an integer as its value.

I only want to have integers to be the value, but when I try converting any string value to a number, it will remove the leading zeroes.

I need to retain the zeroes so that the data sent will reliably be 6 digits, where in the example it would only be 4 digits.

The value is intended to be an identifier for a given Item.

{"Item": [{"ItemName":"007730","BusinessUnit":1}] } 
{"Item": [{"ItemName":917730,"BusinessUnit":1}] }

This is for my work and we use ES6, but if there's an option outside of that, I'm certainly open to it. Thanks in advance!

I am trying to retain leading zeroes in an integer/number within JavaScript.

In this example I have an "ItemName" key which has either a string or an integer as its value.

I only want to have integers to be the value, but when I try converting any string value to a number, it will remove the leading zeroes.

I need to retain the zeroes so that the data sent will reliably be 6 digits, where in the example it would only be 4 digits.

The value is intended to be an identifier for a given Item.

{"Item": [{"ItemName":"007730","BusinessUnit":1}] } 
{"Item": [{"ItemName":917730,"BusinessUnit":1}] }

This is for my work and we use ES6, but if there's an option outside of that, I'm certainly open to it. Thanks in advance!

Share Improve this question asked Nov 26, 2021 at 16:17 Voltaire22Voltaire22 231 silver badge3 bronze badges 4
  • 2 You can only do this as a string. So convert the Number type to a string and pad front with zeros. When you convert this number back to an integer you'll have to remove the leading zeros. Mathematically, leading zeros have no meaning on an integer. They might to humans, hence strings show zeros, but mathematical representations of numeric things won't. – Randy Casburn Commented Nov 26, 2021 at 16:27
  • 1 In addition to what was said/explained by Randy Casburn ... String.prototype.padStart – Peter Seliger Commented Nov 26, 2021 at 16:39
  • 1 btw ... ItemName already strongly hints a string. Anyway it should never get assigned a number value like 917730. – Peter Seliger Commented Nov 26, 2021 at 17:01
  • 2 Thanks for the response! I guess we'll just have to make sure they're all strings across our systems. – Voltaire22 Commented Nov 26, 2021 at 17:04
Add a ment  | 

2 Answers 2

Reset to default 6

You can't have a number with leading zeroes in Javascript, because, as Randy Casburn said, they don't have any value. You have to convert it to a string and use String.padStart() to pad the string with zeroes. parseInt will work with leading zeroes. For example:

(294).toString().padStart(6, "0") --> "000294"


parseInt("000294") --> 294

This can be done with Intl.NumberFormat like this:

const num = 7730;
const formattedNum = new Intl.NumberFormat('en', { minimumIntegerDigits: 6, useGrouping: false }).format(num);
console.log(formattedNum);

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信