If an integer or floating-point value is written to a stream then it is written in the Prolog syntax. This means that a floating-point value like 2.3 is written as the string '2.3' to the stream. With the stream properties number_format/1 and number_format_string/1 it is possible to have more influence about how a number is written to a stream (see set_stream_property/2).
The argument of the number_format_string/1 stream property is an atom that contains characters that tell the stream how to write the number. The valid command characters for number formatting are:
| d=<character> | This command character defines which character is used to seperate the integer and decimal parts of a floating-point number. In the remaining command string this same decimal character must be used. |
| g=<character> | This command character defines which character is used to group integer characters together. This is usefull when a number is very large. In the remaining command string this same group character must be used. |
| 9 | This character is replaced by a number character from the actual value or a whitespace. |
| z | A 'z' character is replaced by a number character from the actual value or the '0' character. |
Characters are truncated from the text representation of a number if it is larger than the maximum allowed number of characters by the command string.
Examples of number format strings are:
| format string: 9999.zz |
| number | formatted number | |
| 12 | 12.00 | two extra zero characters are appended after the decimal point |
| 12.1 | 12.10 | only one extra zero character is appended |
| 12.123 | 12.12 | the last character '3' is truncated |
| 62367 | 2367.00 | the first character '6' is truncated because it is not allowed. The format string states that the maximum size of the integer part of a number is 4 characters. |
The following example demonstrates a command string with a group character and a different decimal character.
| format string: d=,g=.999.999.zzz (this format string defines ',' as the decimal symbol and '.' becomes the group symbol) |
| number | formatted number | |
| 12 | 12,000 | three extra zero characters are appended after the , character |
| 12.1 | 12,100 | two extra zero character are appended |
| 712.123 | 712,123 | the last character '3' is truncated |
| 62367 | 62.367,000 | the group symbol is inserted as the third character and a decimal character with three zeros are appended |
The string used for number formatting can be queried by using the predicate stream_property/2. For instance:
:- stream_property(user_output, number_format_string(O)). |
While a stream is open the number format string can be changed and it is also possible to turn number formatting on or off. The stream property number_format/1 can be used for that. Turning number formatting off for the stream 'user_output' can be done by the command:
:- set_stream_property(user_output, number_format(false)). |