Reputation: 8584
In order to "pad" a number I'm printing so that it's always a fixed number of characters, I'm making a padding string based off how many integers and in the given number:
pad := ' '.
(freqVal < 10) ifTrue: [ pad := ' ' ].
((freqVal < 100) & (freqVal > 9)) ifTrue: [ pad := ' ' ].
((freqVal < 1000) & (freqVal > 99)) ifTrue: [ pad := ' ' ].
stdout<<pad<<freqVal<<<<nl
However, the printed result always makes the variable pad
into a letter instead of spaces like I'm assigning its value to. If I add pad displayNl
before the last line it prints out a letter for some reason instead of just spaces.
Any ideas why this might be occurring?
Upvotes: 0
Views: 118
Reputation: 168
I don't know Gnu-Smalltalk in particular. Surely there are some handy String methods or formatters you could reuse for this purpose though. My advice would be to first convert the number into a String and then format it with blank-padding. That way you will avoid type conversion Problems which you've experienced
new String method (preferrably an existing one in your ST Distribution):
withLeading: aCharacter size: anInteger
(anInteger < self size) ifTrue: [^self copyFrom: 1 to: anInteger].
^((self species new: anInteger - self size) atAllPut: aCharacter ), self
usage example
9 asString withLeading: ($ ) size: 10 "result ' 9'"
10 asString withLeading: ($ ) size: 10 "result ' 10'"
999 asString withLeading: ($ ) size: 10 "result ' 999'"
Upvotes: 2