æ°å¤ã®å é ã«0ã追å ãã¦æ¡ããããã
æ°å¤ã®å é ã«0ã追å ãã¦æ¡ãããããé¢æ°ãæ¸ãã¾ããã
/** * å¿ è¦ãªæ¡æ°ã¾ã§0ãåããã * @param number æ°å¤ * @param size æ¡æ° */ function fillZero( number, size ) { var s = Math.log( number ) * Math.LOG10E; for( i=1,n=size-s,str="";i<n;i++ ) str += "0"; return str+number; }
ãµã³ãã«ã¯ä»¥ä¸ã
var out = document.getElementById("out"); out.innerHTML += "3, 1 : " + fillZero( 3, 1 ) + "<br/>"; out.innerHTML += "3, 2 : " + fillZero( 3, 2 ) + "<br/>"; out.innerHTML += "3, 3 : " + fillZero( 3, 3 ) + "<br/>"; out.innerHTML += "23, 3 : " + fillZero( 23, 3 ) + "<br/>"; out.innerHTML += "123, 3 : " + fillZero( 123, 3 ) + "<br/>"; out.innerHTML += "1234, 3 : " + fillZero( 1234, 3 ) + "<br/>"; out.innerHTML += "-3, 2 : " + fillZero( -3, 2 ) + "<br/>"; out.innerHTML += "3.2, 2 : " + fillZero( 3.2, 2 ) + "<br/>";
ããã¦ããããæ¸ããå¾ãããªã®ãã¿ã¤ããã
ânew Error(”exeception throw”) - "先頭を0で埋めて桁をそろえる" を考えてみた
ãªãã»ã©ãæååã¨ãã¦æ±ãã®ããããã§ããããªã¼ããµããµãã