TOSTR$: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
{{PageParameters}} | {{PageParameters}} | ||
* {{Parameter|value}} is any numerical type value to convert. | * {{Parameter|value}} is any [[Variable Types|numerical type]] value to convert, literal or variable. | ||
* {{Parameter|digits}} is optional, if given it determines the maximum number of significant digits to convert for floating point values. | * {{Parameter|digits}} is optional, if given it determines the maximum number of significant digits to convert for floating point values. | ||
** This argument has no effect for integer values and is silently ignored, if given. | ** This argument has no effect for integer values and is silently ignored, if given. | ||
Line 15: | Line 15: | ||
** Negative digit numbers will cause an "'''Illegal function call'''" error. | ** Negative digit numbers will cause an "'''Illegal function call'''" error. | ||
** The digits exceeding the given maximum are not simply cut off, but rounded to the final position converted. | ** The digits exceeding the given maximum are not simply cut off, but rounded to the final position converted. | ||
*** If rounding results | *** If rounding results into a trailing ''zero(0)'', then it is omitted. | ||
Line 99: | Line 99: | ||
{{Cl|PRINT}} {{Text|<nowiki>"(cropped to max. digits for "</nowiki>|#FFB100}}; typ$; {{Text|<nowiki>")"</nowiki>|#FFB100}} | {{Cl|PRINT}} {{Text|<nowiki>"(cropped to max. digits for "</nowiki>|#FFB100}}; typ$; {{Text|<nowiki>")"</nowiki>|#FFB100}} | ||
{{Cl|ELSEIF}} x% > {{Text|1|#F580B1}} {{Cl|_ANDALSO}} {{Cl|INSTR}}(num$, {{Text|<nowiki>"."</nowiki>|#FFB100}}) > {{Text|0|#F580B1}} {{Cl|_ANDALSO}} {{Cl|LEN}}(num$) < x% + {{Text|1|#F580B1}} {{Cl|THEN}} | {{Cl|ELSEIF}} x% > {{Text|1|#F580B1}} {{Cl|_ANDALSO}} {{Cl|INSTR}}(num$, {{Text|<nowiki>"."</nowiki>|#FFB100}}) > {{Text|0|#F580B1}} {{Cl|_ANDALSO}} {{Cl|LEN}}(num$) < x% + {{Text|1|#F580B1}} {{Cl|THEN}} | ||
{{Cl|PRINT}} {{Text|<nowiki>"(trailing zero (rounding) | {{Cl|PRINT}} {{Text|<nowiki>"(trailing zero (rounding) omitted)"</nowiki>|#FFB100}} | ||
{{Cl|ELSE}} | {{Cl|ELSE}} | ||
{{Cl|PRINT}} | {{Cl|PRINT}} | ||
Line 123: | Line 123: | ||
11 digits: 31.415926536 | 11 digits: 31.415926536 | ||
12 digits: 31.4159265359 | 12 digits: 31.4159265359 | ||
13 digits: 31.4159265359 (trailing zero (rounding) | 13 digits: 31.4159265359 (trailing zero (rounding) omitted) | ||
14 digits: 31.415926535898 | 14 digits: 31.415926535898 | ||
15 digits: 31.4159265358979 | 15 digits: 31.4159265358979 |
Latest revision as of 23:01, 21 November 2024
The _TOSTR$ function returns the STRING representation of a numerical value. It's a successor of the legacy STR$ function.
Syntax
- result$ = _TOSTR$(value[, digits])
Parameters
- value is any numerical type value to convert, literal or variable.
- digits is optional, if given it determines the maximum number of significant digits to convert for floating point values.
- This argument has no effect for integer values and is silently ignored, if given.
- For SINGLE values 1-7 digits are possible, for DOUBLE values 1-16 digits and for _FLOAT values 1-19 digits.
- Digit numbers exceeding the possible range are clipped to either the minimum or maximum possible.
- Negative digit numbers will cause an "Illegal function call" error.
- The digits exceeding the given maximum are not simply cut off, but rounded to the final position converted.
- If rounding results into a trailing zero(0), then it is omitted.
Description
- Different from the legacy STR$ this function will not add a leading space to positive values, hence you can waive to the usual trimming using LTRIM$ or _TRIM$.
- While the legacy STR$ can only handle _FLOAT values as long as they not exceed the DOUBLE range, this function supports the full _FLOAT range.
Availability
Examples
- Example 1
- Showing the minimum and maximum of different floating point types.
'min/max values for SINGLE, DOUBLE and _FLOAT smi! = -3.402823E+38 smf! = 1.175494E-38 sma! = 3.402823E+38 dmi# = -1.797693134862315D+308 dmf# = 2.225073858507201D-308 dma# = 1.797693134862315D+308 fmi## = -1.189731495357231765F+4932 fmf## = 3.362103143112093506F-4932 fma## = 1.189731495357231765F+4932 PRINT "Values enclosed by () to show there's no leading/trailing space." PRINT PRINT "min. SINGLE: ("; _TOSTR$(smi!); ")" PRINT "min. fraction: ("; _TOSTR$(smf!); ")" PRINT "max. SINGLE: ("; _TOSTR$(sma!); ")" PRINT PRINT "min. DOUBLE: ("; _TOSTR$(dmi#); ")" PRINT "min. fraction: ("; _TOSTR$(dmf#); ")" PRINT "max. DOUBLE: ("; _TOSTR$(dma#); ")" PRINT PRINT "min. _FLOAT: ("; _TOSTR$(fmi##); ")" PRINT "min. fraction: ("; _TOSTR$(fmf##); ")" PRINT "max. _FLOAT: ("; _TOSTR$(fma##); ")" END |
Values enclosed by () to show there's no leading/trailing space. min. SINGLE: (-3.402823E+38) min. fraction: (1.175494E-38) max. SINGLE: (3.402823E+38) min. DOUBLE: (-1.797693134862315D+308) min. fraction: (2.225073858507201D-308) max. DOUBLE: (1.797693134862315D+308) min. _FLOAT: (-1.189731495357231765F+4932) min. fraction: (3.362103143112093506F-4932) max. _FLOAT: (1.189731495357231765F+4932) |
- Example 2
- Showing how the optional digits argument affects the result.
num## = _PI(10): typ$ = "_FLOAT": mi% = 1: ma% = 19 PRINT "Printing _PI(10) as "; typ$; ":" PRINT " default: "; _TOSTR$(num##) FOR x% = 0 TO 20 PRINT USING "## digits: "; x%; num$ = _TOSTR$(num##, x%) PRINT num$; SPC(ma% + 5 - LEN(num$)); IF x% < mi% THEN PRINT "(forced to at least 1 digit)" ELSEIF x% > ma% THEN PRINT "(cropped to max. digits for "; typ$; ")" ELSEIF x% > 1 _ANDALSO INSTR(num$, ".") > 0 _ANDALSO LEN(num$) < x% + 1 THEN PRINT "(trailing zero (rounding) omitted)" ELSE PRINT END IF NEXT x% END |
Printing _PI(10) as _FLOAT: default: 31.41592653589793116 0 digits: 3F+01 (forced to at least 1 digit) 1 digits: 3F+01 2 digits: 31 3 digits: 31.4 4 digits: 31.42 5 digits: 31.416 6 digits: 31.4159 7 digits: 31.41593 8 digits: 31.415927 9 digits: 31.4159265 10 digits: 31.41592654 11 digits: 31.415926536 12 digits: 31.4159265359 13 digits: 31.4159265359 (trailing zero (rounding) omitted) 14 digits: 31.415926535898 15 digits: 31.4159265358979 16 digits: 31.41592653589793 17 digits: 31.415926535897931 18 digits: 31.4159265358979312 19 digits: 31.41592653589793116 20 digits: 31.41592653589793116 (cropped to max. digits for _FLOAT) |
See also