0
0

More than 1 year has passed since last update.

ゼロパディング

Posted at

ゼロパディング

概要

ゼロパディングとは、画面や帳票などで数値を表現する際、書式で指定した桁数に満たない部分をゼロで埋めることである。

ゼロパディングの「パディング」には埋める、詰め物をするといった意味がある。

ゼロパディングは、例えば「123」を5桁で表す場合、2桁足りない部分をゼロで埋めて「00123」と表記する。もともと5桁の数であればゼロを埋めるスペースがないのでそのままの数を表記する。

ゼロパディング

スクリプト

padStart使用の場合

const str1 = '5';

console.log(str1.padStart(5, '0'));

// 5桁になるように0で埋める
// 00005

slice使用の場合

var num = 5;
console.log(('00000' + 5).slice(-5));

// 5桁になるように0で埋める
// 00005
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0