DEV Community

loading...

Pretty format numbers

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read
const length = 5;
const amount = [23, 435, 34.34, 234.3];

amount.forEach(num => {
    const paddedNum = String(num).padStart(length, '0');
    console.log(paddedNum);
});

/* 
Output:
00023
00435
34.34
234.3
*/

Enter fullscreen mode Exit fullscreen mode

Thanks for reading 💙

Follow @codedrops.tech for daily posts.

Instagram ● Twitter ● Facebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (0)

pic
Editor guide