LoginSignup
14
16

More than 5 years have passed since last update.

JavaScriptでsprintf

Last updated at Posted at 2013-07-11

少し探したが、これがよさそう
http://homepage3.nifty.com/aya_js/js_sub/sprintf.htm

以下引用


sprintf("%d", 12345)); 12345 //符号付き10進整数
sprintf("%d", 123.6)); 123 //四捨五入でなく切り捨て
sprintf("%6d", 123)); 123 //幅指定
sprintf("%06d", 123)); 000123 //flag 0
sprintf("%06d", -123)); -00123 //先頭は -
sprintf("%6.4d", 123)); 0123 //整数の精度
sprintf("%6.8d", 123)); 00000123 //精度優先
sprintf("%f", 123.45)); 123.450000 //小数 デフォルトの精度は6
sprintf("%11f", 123.45)); 123.450000 //幅指定
sprintf("%.3f", 123.45)); 123.450 //精度指定
sprintf("%7.1f", 123.25)); 123.2 //四捨五入ではない
sprintf("%7.1f", 123.251)); 123.3 //5より大きければ切り上げ
sprintf("%7.0f", 123.45)); 123 //精度0なら.無し
sprintf("%08.2f", 123.45)); 00123.45 //flag 0
sprintf("%08.2f", -123.45)); -0123.45 //先頭は -
sprintf("%E", 123.45)); 1.234500E+02 //指数 デフォルトの精度は6
sprintf("%13E", 123.45)); 1.234500E+02 //幅指定
sprintf("%.4E", 123.45)); 1.2345E+02 //精度指定
sprintf("%13.5e", 123.45)); 1.23450e+02 //指数e
sprintf("%s", "abc")); abc //文字列
sprintf("%4s", "abc")); abc //幅指定
sprintf("%.2s", "abc")); ab //精度指定
sprintf("%4.s", "abc")); //精度 0
sprintf("%d", 123.45)); 123 //小数は整数にして表示
sprintf("%d", "123")); 123 //文字列も整数にして表示
sprintf("%d", "abc")); 0 //数値と解釈できないときは0
sprintf("%7s", 123.45)); 123.45 //数値を文字列に変換
sprintf("%-7d", 123.45)); 123 //左詰め
sprintf("% f", 123.45)); 123.450000 //空白追加
sprintf("%+7.3f", 123.45)); +123.450 //+追加
sprintf("%+- 9.2fa", 123.45)); +123.45 a //複数のオプション

===追記===
↑のを使うとけっこうNaNが出るので、
↓の方がよさそう

14
16
0

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
14
16