isama92

Javascript str_pad of zeroes

Mar 17th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************************************
  2. var number, str, ndigits;
  3.  
  4. ndigits = 5;    // you want it to have 5 digits
  5. number = 23;    // this is your number
  6. str = digits(number, ndigits); // str == '00023'       this is your result
  7.  
  8. number = 936;
  9. ndigits = 10;
  10. str = digits(number, ndigits); // str == '0000000936'
  11. ************************************/
  12.  
  13. function digits(n,ndigit){
  14.     return (n.length < ndigit ? digits("0"+n, ndigit) : n);
  15. }
Advertisement
Add Comment
Please, Sign In to add comment