sumim #3098(2007/09/19 10:02 GMT) [ Smalltalk ] Rating0/0=0.00
1 2 3
World findATranscript: nil. 1 to: 9 do: [:x | 1 to: 9 do: [:y | Transcript cr; show: ('{1} * {2} = {3}' format: {x. y. (x * y) printPaddedWith: $ to: 2})]]
Rating0/0=0.00-0+
1 reply [ reply ]
sumim #3099(2007/09/19 10:07 GMT) Rating0/0=0.00
[ reply ]
sumim
#3098()
[
Smalltalk
]
Rating0/0=0.00
#format: は単に文字列指定箇所への埋め込みのみで、printf の書式指定のような細やかな機能はないので、数値の文字列変換に際し、要求にあわせてあらかじめ整形しておく必要があります。
今回は、第一引数の文字で第一引数の文字数に足りない分を左側から追加して埋める #printPaddedWith:to: というメソッドを使用しました。
なお、$ は Smalltalk での文字オブジェクトリテラルに冠する記号で、直後に続く一文字(下の場合、スペース)に相当する文字オブジェクトを返します。
World findATranscript: nil. 1 to: 9 do: [:x | 1 to: 9 do: [:y | Transcript cr; show: ('{1} * {2} = {3}' format: {x. y. (x * y) printPaddedWith: $ to: 2})]]Rating0/0=0.00-0+
1 reply [ reply ]