===== Displaying and formatting numbers =====
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 thousands separators and also grouping the fractional digits |
| ''1.23456789123456789E8'' | Using 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 (a.k.a. __normal numbers__ in this discussion).
The default configuration presents numbers less than ''1E-12'' in scientific notation, numbers greater than ''1E+12'' 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 ''[[manual:chapter6:flags:cmd_setnfmt|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 below);
* passing a single positive real number will change the cutoff limits without altering the format itself. If the number is greater than ''1'', the limit for large numbers will be changed. Likewise, if the number is less than ''1'' the limit for small numbers will be changed;
* 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 greater or less than ''1'').
* if the list contains one or more strings 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 the list contains a cutoff limit followed by format string, the number will change the cutoff limit according to its magnitude, 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 __normal → small numbers → large numbers__.
Here are some examples:
^ ''[[manual:chapter6:flags:cmd_setnfmt|SETNFMT]]'' argument ^ Effect ^
| ''"#.##"'' | **Small numbers format:** ''#.##''\\ **Small numbers cutoff:** unchanged\\ **Normal numbers format:** ''#.##''\\ **Large numbers cutoff:** unchanged\\ **Large numbers format:** ''#.##'' |
| ''1E-10'' | **Small numbers format:** unchanged\\ **Small numbers cutoff:** ''1E-10''\\ **Normal numbers format:** unchanged\\ **Large numbers cutoff:** unchanged\\ **Large numbers format:** unchanged |
| ''{ 1E+15 "#.#0E" }'' | **Small numbers format:** unchanged\\ **Small numbers cutoff:** unchanged\\ **Normal numbers format:** unchanged\\ **Large numbers cutoff:** ''1E+15''\\ **Large numbers format:** ''#.#0E'' |
| ''{ "#.###" "#.###0E" "#.#0E"}'' | **Small numbers format:** ''#.###0E''\\ **Small numbers cutoff:** unchanged\\ **Normal numbers format:** ''#.###''\\ **Large numbers cutoff:** unchanged\\ **Large numbers format:** ''#.#0E'' |
| ''{ 1E-10 "#.##E#" "#.##" }'' | **Small numbers format:** ''#.##E#''\\ **Small numbers cutoff:** ''1E-10''\\ **Normal numbers format:** unchanged\\ **Large numbers cutoff:** unchanged\\ **Large numbers format:** ''#.##'' |
Regarding the last example, the first element is ''1E-10'' therefore the string immediately following will set the format for small numbers (since ''1E-10'' < ''1'') to ''#.##E#''. The following string will change the large numbers, since no limit is specified and therefore the sequence __normal → small numbers → large numbers__ is followed. If there were a third string afterwards, it would have changed 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 default format is:
{
"#.12#." @ Normal
1E-12 "#.12#.E*" @ Small numbers (< 1E-12)
1E12 "#.12#.E*" @ Large numbers (> 1E+12)
}
Current format list can be retrieved using the ''[[manual:chapter6:flags:cmd_getnfmt|GETNFMT]]'' command.
----
==== The format string ====
The format string defines how a number is displayed; in its basic form is very simple, but the syntax allows for great flexibility and its appearance can become a bit cryptic... at a first glance the structure of the string is:
| ''integer_part[fractional_part][approximation_dot][exponent_part]'' |
Anything enclosed in ''[ ]'' is optional, the brackets ''< >'' and the vertical bar ''|'' separate mutually exclusive choices and the ellipsis ''...'' means repetition of a character. Now we can proceed examining each part separately.
----
=== The INTEGER part ===
The //integer part// of the format string has the following format:
| ''[+][S[n]]#'' |
The simplest format string is just the hash sign ''"#"''. In fact that's the only mandatory part: a single ''#'' 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 plus sign ''+'' preceding the hash sign will force the display of the sign even for positive numbers. The negative sign will always be displayed;
* the uppercase ''S'' preceding the hash sign 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 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 ''[[manual:chapter6:flags:cmd_setlocale|SETLOCALE]]''.
^ Format ^ Number ^ Displayed as ^
| ''#'' | 123.456 | ''123'' |
| ''+#'' | 123.456 | ''+123'' |
| ''S4#'' | 123456.789 | ''12 3457'' |
| ''S3#'' | 123456.789 | ''123 457'' |
----
=== The FRACTIONAL part ===
The //fractional part// of the format string has the following format:
| ''.<#...|d#|A#>[0][S[n]]'' |
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 (a.k.a. //radix point//), even if the decimal separator defined by the ''[[manual:chapter6:flags:cmd_setlocale|SETLOCALE]]'' is a different character.
The number of digits desired can be expressed in three different ways:
* by the number of ''#'' symbols;
* by including the actual number of digits preceding the ''#'';
* by using ''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.
Various other options are available to format the fractional part of the number:
* when the number of decimal digits is expressed by a repetition of ''#'''s, replacing the last ''#'' with a ''0'' will append as many trailing zeros as needed to display the requested number of decimal figures. The ''0'' character counts as one additional digit.
* when the number of decimal digits is expressed by a number //d// followed by a ''#'', a trailing ''0'' still indicates that trailing zeros need to be added **but** in this case the presence of ''0'' does not add another digit.
* the uppercase ''S'' following the last ''#'' or ''0'' 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 add the fractional separator every 4 digits. Valid values for ''S'' are ''1'' through ''15'', and the separator character used will be as defined by the command ''[[manual:chapter6:flags:cmd_setlocale|SETLOCALE]]''.\\ **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//.
^ Format ^ Number ^ Displayed as ^
| ''#.####'' | 123.45678 | ''123.4568'' |
| ''#.4#'' | 123.45678 | ''123.4568'' |
| ''#.A#'' | 123.45678 | ''123.45678'' |
| ''#.###0'' | 123.45 | ''123.4500'' |
| ''#.4#0'' | 123.45 | ''123.4500'' |
| ''#.8#0S4'' | 123.45 | ''123.4500 0000'' |
----
=== The APPROXIMATION dot ===
The //approximation dot part// of the format string has the following format:
| ''[.]'' |
In **newRPL** the real numbers can be represented as [[manual:chapter3:reals#approximate-versus-exact|exact]] or [[manual:chapter3:reals#approximate-versus-exact|approximate]], the latter property indicated by a trailing dot ''.'' following the fractional (or just the integer, if missing) part of the number.
It is possible to turn on or off the display of the approximation dot adding a single dot ''.'' character just after the integer and fractional part of the format.
**NOTE**: the approximation dot is always displayed as a dot: the command ''[[manual:chapter6:flags:cmd_setlocale|SETLOCALE]]'' can change the appearance of the decimal separator only.
^ Format ^ Number ^ Displayed as ^
| ''#.####'' | 123.45678. | ''123.4568'' |
| ''#.####.'' | 123.45678. | ''123.4568.'' |
----
=== The EXPONENT part ===
The //exponent part// of the format string has the following format:
| ''[*][+][]'' |
To display a number in __scientific__ or __engineering__ notation (with an optional preferred exponent) the exponent letter ''E'' or ''e'' is included in the format string after the fractional part. The difference between the two notations is the following:
* in __scientific__ notation the integer part is a single digit number from ''0'' to ''9'';
* in __engineering__ notation the integer part is a number from ''0'' to ''999'';
* in __engineering__ notation with a preferred exponent the integer part is chosen such as the exponent is as specified.
Various other options are available to format the exponent part of the number:
* an asterisk ''*'' following the exponent letter will suppress the exponent in scientific notation when the exponent is ''0'';
* a plus sign ''+'' after the exponent letter will force the exponent sign for positive exponents. Negative exponents are always displayed with the sign;
* when the hash symbol ''#'' is present after the exponent letter, it activates engineering notation. 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'';
* when a number is present after the exponent letter, it activates engineering notation and indicates a preferred exponent. All numbers will display using the requested exponent. The preferred exponent can be in the range ''-21'' to ''+21'', and has to be a __multiple of 3__.\\ **NOTE**: When using engineering notation with a preferred exponent, the asterisk will **always** suppress the exponent and not only when it's ''0''.
^ Format ^ Number ^ Displayed as ^
| ''#.##.E'' | 0.01234. | ''1.23.E-2'' |
| ''#.##.e'' | 0.01234. | ''1.23.e-2'' |
| ''#.##.E'' | 3.5 | ''3.5E0'' |
| ''#.##.E+'' | 3.5 | ''3.5E+0'' |
| ''#.##.E*'' | 3.5 | ''3.5'' |
| ''#.##.E+#'' | 35150 | ''35.15E+3'' |
| ''#.##.E+3'' | 35 | ''0.04E+3'' |
| ''#.##.E*+3'' | 35 | ''0.04'' |
----
==== Format localization ====
The ''[[manual:chapter6:flags:cmd_setnfmt|SETNFMT]]'' command allows great customization with respect to the grouping of digits in the integer and fractional parts of the number; the command ''[[manual:chapter6:flags:cmd_setlocale|SETLOCALE]]'' allows customization of the characters that delimit these groupings.
''[[manual:chapter6:flags:cmd_setlocale|SETLOCALE]]'' accepts a 4-character string which defines what character must be used for each grouping: the following table shows the default setting.
^ Position ^ Character ^ Function ^ Notes ^
| **1** | ''.'' | Decimal separator | This is the character used to divide the integer part of a number from the fractional part. This character is **NOT** the same as the [[manual:chapter3:reals#approximate-versus-exact|approximation dot]] which is always displayed as a dot. |
| **2** | **U**+2009\\ (thin space) | Integer separator | This is the character used to divide the digits groups in the integer part of a number. |
| **3** | **U**+2009\\ (thin space) | Fractional separator | This is the character used to divide the digits groups in the fractional part of a number. |
| **4** | '','' | Arguments separator | This is the character used to divide the arguments of a symbolic function (e.g. ''%%'%%GCD(16,32)%%'%%'') or the real and imaginary parts of a complex number (e.g. ''(1,-2)''). |
The ''[[manual:chapter6:flags:cmd_getlocale|GETLOCALE]]'' command returns on the stack the localization string.
----
==== Preset formats ====
A different, user friendly way to change the display of numbers is to use the preset formats that are provided by **newRPL**. These presets are quickly accessed via the keyboard and can be combined in many different ways:
* **ONhold-SP** cycles thru number format presets;
* **ONhold-DOT** cycles thru localization presets;
* **ONhold-{1...9}** selects the length of the fractional part in scientific or engineering mode;
* **ONhold-{MUL/DIV}** switches to engineering mode and selects ''Auto'' (no preferred exponent) or a preferred exponent in the range ''-21'' to ''21'' in steps by 3.
\\ \\
**__Base number format presets__**
^ Preset name ^ Small numbers format ^ Small numbers cutoff ^ Normal numbers format ^ Large numbers cutoff ^ Large numbers format ^
| **STD** | ''#.12#.E*'' | ''1E-12'' | ''#.12#.'' | ''1E+12'' | ''#.12#.E*'' |
| **FIX** | ''#.12#.E*'' | ''1E-12'' | ''#.12#0.'' | ''1E+12'' | ''#.12#.E*'' |
| **SCI** | ''#.12#.E*'' | ''1E-12'' | ''#.12#.E'' | ''1E+12'' | ''#.12#.E*'' |
| **ENG** | ''#.12#.E*'' | ''1E-12'' | ''#.12#.E*#'' | ''1E+12'' | ''#.12#.E*'' |
\\ \\
**__Base localization presets__**
^ Preset name ^ Integer grouping ^ Fractional grouping ^ Decimal separator ^ Integer separator ^ Fractional separator ^ Arguments separator ^
| ''1000.000000'' | **No** | **No** | ''.'' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '','' |
| ''1,000.000000'' | **Yes** | **No** | ''.'' | '','' | **U**+2009\\ (thin space) | '';'' |
| ''1 000.000000'' | **Yes** | **No** | ''.'' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '','' |
| ''1000.000 000'' | **No** | **Yes** | ''.'' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '','' |
| ''1,000.000 000'' | **Yes** | **Yes** | ''.'' | '','' | **U**+2009\\ (thin space) | '';'' |
| ''1 000.000 000'' | **Yes** | **Yes** | ''.'' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '','' |
| ''1000,000000'' | **No** | **No** | '','' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '';'' |
| ''1.000,000000'' | **Yes** | **No** | '','' | ''.'' | **U**+2009\\ (thin space) | '';'' |
| ''1 000,000000'' | **Yes** | **No** | '','' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '';'' |
| ''1000,000 000'' | **No** | **Yes** | '','' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '';'' |
| ''1.000,000 000'' | **Yes** | **Yes** | '','' | ''.'' | **U**+2009\\ (thin space) | '';'' |
| ''1 000,000 000'' | **Yes** | **Yes** | '','' | **U**+2009\\ (thin space) | **U**+2009\\ (thin space) | '';'' |