Trick Zero Pad Numbers

There are many ways to add  zero in font of the number (zero pad)  in java script, php, and postgres.  I just found little trick and really simple. Let's say we need to get format a number with leading zero, e.g: total digits of number will be 4.


Java Script:
  
var no= 28;
number= String("0000" + no).slice(-4);  // result 0028
PHP:
 
$no = "28";
echo str_pad($no, 4, '0', STR_PAD_LEFT);  //result 0028

Postgres Sql:

 
select (substring('0000' || CAST(CAST (01 AS int) as varchar) from '....$'))
from m_order 


 It is also applicable to padding with any character, not just zeros.

Komentar

Postingan populer dari blog ini

Pivot Tabel Postgres

Numbers to Words Bahasa Indonesia dengan PHP

Seputar Post Polio