In my angular app, assume I have a string value 00065. I need to convert this to a number and add + 1. i.e; 00065 + 1, which is 00066 Same way if the number is 00120, I should do 00120 +1 which is 00121. I don't want the preceeding zeros not be to ignored. As soon as I convert it to number using Number, the preceeding zeros are removed. how can I acheive this.
Asked
Active
Viewed 146 times
-1
-
Possible duplicate of [adding zeros in front of a string](https://stackoverflow.com/questions/30490968/adding-zeros-in-front-of-a-string) – sevic Jul 14 '19 at 05:50
2 Answers
1
Create method
pad(n) { return ("000000" + n).slice(-6); }
Call this method in ur model.
check this link out @Roberto's answer
here is the example, check this out: https://stackblitz.com/edit/angular-adzgcl

rajhim
- 259
- 2
- 14
0
Convert the string using parseInt
, then if you don't need IE support you can use the String.prototype.padStart()
method.
For example:
function paddedAddOne(value) {
const nextValue = parseInt(value, 10) + 1;
return `${nextValue}`.padStart(5, '0')
}
Otherwise you'll need to add a polyfill.
References:

DDeis
- 316
- 3
- 9