How to convert an integer to a string for a single number as well as for multiple number in an array?

Not applicable
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 10:29 AM
How to convert an integer to a string for a single number as well as for multiple number in an array?
Example: 1
var x1 = 001;
Example: 2
var x2 = [001,002,003,004,005];
How to convert x1 to '001' and x2 to ['001','002','003','004','005']
Labels:
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 10:34 AM
you can try following.
var x1 = 001;
var x1_string = "00" + x1.toString();
---------
var x2 = [001,002,003,004,005];
var x2_string = [];
for(i=0;i<x2.toString().split(",").length;i++)
{
x2_string.push("00" + x2[i].toString());
}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2022 12:27 AM
Check on this stackoverflow post. You can loop through your array of numbers and use the below function.
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
gs.info(pad(10, 4)); // 0010
gs.info(pad(9, 4)); // 0009
gs.info(pad(123, 4)); // 0123
gs.info(pad(10, 4, '-')); // --10