LoginSignup
1
0

More than 5 years have passed since last update.

Smalltalk(Pharo) である桁ごとにコンマ区切りにする

Last updated at Posted at 2018-08-28

 ふと思い立ってある桁(以下の場合は3桁)ごとにコンマ区切りにするコードを書いてみました。
 もっとスマートにやるというか、そのものズバリの何かがあるような気がするのですが…。

 (追記)コメントにある通り Integer >> printSeparatedBy: every: signed: base: on: がそのものズバリのようです。やっぱり、ありそうなものはあるものですね。

|n stream|
n := 1.
stream := WriteStream with: ''.
1000000 asString reverse do: [:s | stream << s.
    (n = 3) ifTrue: [
        stream << ','.
        n := 1.
        ]
    ifFalse: [
        n := n + 1
        ].
    ].

Transcript show: stream contents reverse; cr.
1
0
2

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
1
0