String formatting with printf:: Difference between revisions
Jump to navigation
Jump to search
(importe material) |
No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
'''Problems'''. |
|||
⚫ | |||
* How can I print out a text table? |
|||
* How can I format numbers with only 2 decimals? |
|||
⚫ | |||
* ... |
|||
'''Solution'''. These and more little everyday problems can be solved with <code>String>>printf:</code> . See the examples below. |
'''Solution'''. These and more little everyday problems can be solved with <code>String>>printf:</code> . See the examples below. |
||
<syntaxhighlight lang="smalltalk"> |
|||
"to use :printf you must load this package" |
|||
Feature require:'Printf' |
Feature require:'Printf' |
||
</syntaxhighlight> |
|||
<syntaxhighlight lang="smalltalk"> |
|||
". print an integer" |
". print an integer" |
||
'a number : %d' printf: 123. " 'a number : 123' " |
'a number : %d' printf: 123. " 'a number : 123' " |
||
". pad an integer with starting zeros to make it a deault lenght" |
". pad an integer with starting zeros to make it a deault lenght" |
||
'padding a number: %05d' printf: 123. " 'padding a number: 00123' " |
'padding a number: %05d' printf: 123. "=> 'padding a number: 00123' " |
||
". print a floating point with as many decimals you wish" |
". print a floating point with as many decimals you wish" |
||
'3 decimals: %.3f' printf: 123.456789. " '3 decimals: 123.456' " |
'3 decimals: %.3f' printf: 123.456789. "=> '3 decimals: 123.456' " |
||
". fixed length strings, good for tables " |
". fixed length strings, good for tables " |
||
'%15s %15s %10s' printf: {'Name'. 'Last name'. 'mail'} |
'%15s %15s %10s' printf: {'Name'. 'Last name'. 'mail'}. |
||
"=> ' Name Last name mail' " |
|||
". fixed length strings, align left, good for tables " |
". fixed length strings, align left, good for tables " |
||
'%-15s %-15s %-10s' printf: {'Name'. 'Last name'. 'mail'}. |
'%-15s %-15s %-10s' printf: {'Name'. 'Last name'. 'mail'}. |
||
"=> 'Name Last name mail ' " |
|||
". classic newline " |
". BROKEN-at-2025-05-05 --- classic newline " |
||
'line1\nline2' printf: nil. "=> 'line1 |
'line1\nline2' printf: nil. "=> 'line1 |
||
line2' " |
line2' " |
||
</syntaxhighlight> |
|||
* Thousand separators and locales |
* Thousand separators and locales |
||
<syntaxhighlight lang="smalltalk"> |
|||
". print an integer with thousand separators " |
|||
'%_d' printf: {1234}. "=> '1_234' " |
|||
". print a float number with default thousand separator, tested for %f and %d. " |
|||
'%_0.2f' printf: {1234.567}. "=> '1_234.56' " |
|||
". Change format for thousand separator and decimal separator " |
|||
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: $, . |
|||
'%_0.2f' printf: {1234.567}. "=> '1,234.56' " |
|||
". now let's print the number in the way it is considered 'normal' in Italy, for example " |
". now let's print the number in the way it is considered 'normal' in Italy, for example " |
||
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: $. . |
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: $. . |
||
PrintfNumberFormatDescriptor setUserDecimalSeparatorTo: $, . |
PrintfNumberFormatDescriptor setUserDecimalSeparatorTo: $, . |
||
'% |
'%_0.2f' printf: {1234.567}. "=> '1.234,56' " |
||
". and let's put back the default configuaration " |
". and let's put back the default configuaration " |
||
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: nil . |
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: nil . |
||
PrintfNumberFormatDescriptor setUserDecimalSeparatorTo: nil . |
PrintfNumberFormatDescriptor setUserDecimalSeparatorTo: nil . |
||
' |
'_0.2f' printf: {1234.567}. "=> '1_234.56' " |
||
</syntaxhighlight> |
|||
== Things that still do not work or are not implemented == |
== Things that still do not work or are not implemented == |
||
* Formatting a number in scientific notation: `%e`, `%E`, `%g`. |
* Formatting a number in scientific notation: `%e`, `%E`, `%g`. |
||
<syntaxhighlight lang="smalltalk"> |
|||
"?scientifig notation -- broken ?" |
|||
'scientific: %e' printf: 123345.1234. "=> 'scientific: 123345.1' " |
|||
</syntaxhighlight> |
|||
* Formatting a number in scientific notation: `%e`, `%E`, `%g`. |
* Formatting a number in scientific notation: `%e`, `%E`, `%g`. |
||
<syntaxhighlight lang="smalltalk"> |
|||
"?scientifig notation -- broken ?" |
|||
'scientific: %e' printf: 123345.1234. "=> 'scientific: 123345.1' " |
|||
</syntaxhighlight> |
|||
* Formatting a long number splitting it on thousands <code>%'</code>. |
* Formatting a long number splitting it on thousands <code>%'</code>. |
||
* Align a string the center of a fixed space. (this is not available in C but it would be nice to have) |
* Align a string the center of a fixed space. (this is not available in C but it would be nice to have) |
||
----Dr. Nicola Mingotti last |
----Dr. Nicola Mingotti last checked on 05-May-2025. Tested in Cuis rolling release. |
Latest revision as of 19:11, 5 May 2025
Problems.
- How can I print out a text table?
- How can I format numbers with only 2 decimals?
- How can I pad a number to fixed with putting zeros in front of it?
- ...
Solution. These and more little everyday problems can be solved with String>>printf:
. See the examples below.
"to use :printf you must load this package"
Feature require:'Printf'
". print an integer"
'a number : %d' printf: 123. " 'a number : 123' "
". pad an integer with starting zeros to make it a deault lenght"
'padding a number: %05d' printf: 123. "=> 'padding a number: 00123' "
". print a floating point with as many decimals you wish"
'3 decimals: %.3f' printf: 123.456789. "=> '3 decimals: 123.456' "
". fixed length strings, good for tables "
'%15s %15s %10s' printf: {'Name'. 'Last name'. 'mail'}.
"=> ' Name Last name mail' "
". fixed length strings, align left, good for tables "
'%-15s %-15s %-10s' printf: {'Name'. 'Last name'. 'mail'}.
"=> 'Name Last name mail ' "
". BROKEN-at-2025-05-05 --- classic newline "
'line1\nline2' printf: nil. "=> 'line1
line2' "
- Thousand separators and locales
". print an integer with thousand separators "
'%_d' printf: {1234}. "=> '1_234' "
". print a float number with default thousand separator, tested for %f and %d. "
'%_0.2f' printf: {1234.567}. "=> '1_234.56' "
". Change format for thousand separator and decimal separator "
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: $, .
'%_0.2f' printf: {1234.567}. "=> '1,234.56' "
". now let's print the number in the way it is considered 'normal' in Italy, for example "
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: $. .
PrintfNumberFormatDescriptor setUserDecimalSeparatorTo: $, .
'%_0.2f' printf: {1234.567}. "=> '1.234,56' "
". and let's put back the default configuaration "
PrintfNumberFormatDescriptor setUserThousandSeparatorTo: nil .
PrintfNumberFormatDescriptor setUserDecimalSeparatorTo: nil .
'_0.2f' printf: {1234.567}. "=> '1_234.56' "
Things that still do not work or are not implemented
- Formatting a number in scientific notation: `%e`, `%E`, `%g`.
"?scientifig notation -- broken ?"
'scientific: %e' printf: 123345.1234. "=> 'scientific: 123345.1' "
- Formatting a number in scientific notation: `%e`, `%E`, `%g`.
"?scientifig notation -- broken ?"
'scientific: %e' printf: 123345.1234. "=> 'scientific: 123345.1' "
- Formatting a long number splitting it on thousands
%'
. - Align a string the center of a fixed space. (this is not available in C but it would be nice to have)
Dr. Nicola Mingotti last checked on 05-May-2025. Tested in Cuis rolling release.