A number can be displayed on the screen in different ways, regardless of the internal precision or representation of the number. The following are examples of the possible appearance of numbers in newRPL:

123456789.123456789 (this is the default presentation)
123,456,789.123 456 789 (using thousand separators and also grouping the fractional digits)
1.23456789123456789E8 (scientific notation)

Up to 3 different ways to present numbers are used by the system, and they are active at all times. One format is for small numbers (whose absolute magnitude does not exceed a certain limit), another format for large numbers (whose absolute magnitude exceed a certain limit), and a third format for all other numbers in between. The default configuration presents numbers less than 1E-12 in scientific notation, numbers larger than 1E12 in scientific notation, and all others in standard notation. The cutoff limits for small and large numbers can be freely configured, as well as the formatting for each of the three ranges of numbers.

The primary way to change the number formatting is the SETNFMT command. This command accepts a single string or real number, or a list as arguments. Passing a single string changes all 3 formats to the desired format given by the string (the particular format of the string will be explained in short). The list allows fine grain control of what format to change as follows:
1E100 SETNFMT or 1E-10 SETNFMT: Passing a single positive real number will change the cutoff limits without altering the format itself. If the number is larger than one, the limit for large numbers will be changed. Likewise, if the number is less than one the limit for small numbers will be changed.

“#.##” SETNFMT: Passing a single string will change the format for all 3 number ranges to the desired format.

{ 1E100 “#.##” } SETNFMT: A list will change formats for each number range individually. Passing a number and a format string will change the cutoff limit and corresponding format for large or small numbers (depending on the given limit being >1 or <1).

{ 1E-10 “#.##E#” “#.##” } SETNFMT: If the list contains a string without specifying any cutoff limits, the format will be changed in the following order: the first string on the list will change the format for normal numbers, the second one will change small numbers and the third one large numbers. If a format string has a number preceding it, the number will change the cutoff limit, then the string immediately following the number will change the format corresponding to that limit. If a string after has no limit specified, it will follow the order as described. In this example, the list has the number1E-10 therefore it will set the limit for small numbers (since 1E-10 < 1) to the format “#.##E#”. The following string will change the large numbers, since it doesn't have a limit but will follow the sequence “normal → small number → large number”. If there was a string afterwards, it would change the normal numbers. To avoid any ambiguity, it is a good practice to specify the list with the formats in that order, whether they include the cutoff limits or not.

The format string uses the numeral (#) to represent digits. The simplest format string is “#”. The numeral in this case represents the integer part of the number, and its presence on the format string is mandatory. One symbol represents the entire integer part of the number, as many digits as needed.

Other options to format the integer part of the number are as follows:

“+#”: A preceding + symbol will force the display of the sign even for positive numbers. The negative sign will always be displayed.

“S#”: The uppercase S preceding the numeral indicates the presence of a separator. By default the separator is spaced every 3 digits, but that can be controlled by including the number of digits in each group immediately following the S, for example “+S4#” will force the sign and add the thousand separator every 4 digits. Valid values for S are 1 through 15, and the separator character used will be as defined by the command SETLOCALE.

“#.”: A single trailing dot will display the trailing dot for approximate numbers, while no dot will be shown for exact numbers.

To display the fraction part of the number, the string needs to include a decimal dot and at least one # symbol (NOTE: The format string must use a decimal dot, even if the decimal separator set in the Locale is a different character, but the correct character from Locale will be used when displaying numbers). The number of digits desired can be expressed in two different ways, by the number of # symbols, or by including the actual number of digits preceding the #, for example “#.####” or “#.4#” will both display numbers with up to 4 decimal figures.

Various other options are available to format the fractional part of the number:

“#.###0”: Replacing the last # with a zero will append trailing zeros as needed to display the requested number of decimal figures. The zero counts as one additional digit, in this example a total of 4 figures will be displayed.

“#.4#0”: Same as above, after the number of digits, a zero indicates that trailing zeros need to be added. When the number of digits is explicitly given the presence of the zero does not add another digit, in this example a total of 4 will be displayed.

“#.A#”: The A here is replacing the number of decimal figures and means all digits, and will output as many digits as the number has stored. This is useful to store numbers as text or to edit a number, since it allows to recover the same number from the generated text.

“#.###0S2”: The S after the number of digits indicates the fractional digit separator needs to be included, in this case with an optional spacing of 2 digits. When a number of digits is not included, the default is 3 digits (NOTE: the number of digits in a group must match between the integer and fractional part of the number, it's an invalid format string to request separators every 'n' digits on the integer part, and 'm' digits on the fractional part when m≠n). Valid values for S are 1 through 15, and the separator character used will be as defined by the command SETLOCALE.

“#.###0.”: The trailing dot will display the dot on approximate numbers.

“#.#0E”: A number will be displayed in scientific notation when the exponent letter e or E is included in the format string after the fractional part. In this example, 2 decimal figures will be shown (adding trailing zeros as needed) in scientific notation, using a capital E to separate the exponent from the mantissa.

“#.#0E*”: An asterisk following the exponent letter will suppress the exponent in scientific mode when the exponent is zero. For example the number 1.5 will display as 1.50E0 unless the asterisk is present in the format string to suppress it. and show only 1.50.

“#.#0E*+”: A plus sign after the exponent letter will force the exponent sign for positive exponents (negative exponents are always displayed with the sign).

“#.#0E*+3”: When a number is present after the exponent letter, it activates engineering mode, and indicates a preferred exponent. All numbers will display using the requested exponent (engineering mode with a fixed exponent). The preferred exponent can be in the range -21 to +21, and has to be a multiple of 3. When using engineering mode with a preferred exponent, the asterisk will always suppress the exponent (not only when it's zero).

“#.#0E*+#”: When the preferred exponent is expressed as a numeral symbol, it activates engineering mode without a preferred exponent. The displayed number will have an exponent multiple of 3, chosen by the system to ensure the integer part of the mantissa is between 1 and 999.

  • manual/chapter3/numformatting.txt
  • Last modified: 2017/08/13 12:21
  • by claudio