Vimスクリプト(printf)(その7)

テキストエディタ(Vimやその他)
TVアニメ「歌舞伎町シャーロック」第2弾PV

アニメ『歌舞伎町シャーロック』

落語で種明かしする探偵、シャーロック。 1話のつかみは良かったです。シャーロックが食べてたチャーハンかピラフかわかりませんが、桃缶と混ぜてました。美味しいのだろうか気になります。

(親記事はコチラ Vimコマンド&Script入門)

printf続き。今回は、float。

ヘルプ。

                                                        *printf-f* *E807*
                f F     The Float argument is converted into a string of the
                        form 123.456.  The precision specifies the number of
                        digits after the decimal point.  When the precision is
                        zero the decimal point is omitted.  When the precision
                        is not specified 6 is used.  A really big number
                        (out of range or dividing by zero) results in "inf"
                        or "-inf" with %f (INF or -INF with %F).
                        "0.0 / 0.0" results in "nan" with %f (NAN with %F).
                        Example:  
                                echo printf("%.2f", 12.115)
                                12.12
                        Note that roundoff depends on the system libraries.
                        Use |round()| when in doubt.

整数を何桁、 小数を何桁で指定すればいいだけですね。

:let a=23.45
:echo printf("%3.5f", a)

変数を整数にすると、

:let a=23
:echo printf("%3.5f", a)

特に問題なく小数点入りで表示されますね。

文字列だとどうでしょうか。

:let a="HELLO"
:echo printf("%3.5f", a)

やはりエラーになりますね。

整数部は省略できるか。

:let a=23.45
:echo printf("%.5f", a)

コメント

タイトルとURLをコピーしました