ftostr Function
Function: |
Converts real value into its string representation. |
Syntax: |
ftostr(byref num as real, mode as ftostr_mode, rnd as byte) as string |
See Also: |
Part |
Description |
num |
Real value to convert. |
mode |
Desired output format: 0 — FTOSTR_MODE_AUTO: Choose between plain and mantissa/exponent format automatically. If the mantissa/exponent format results in a shorter string, it will be used, otherwise the plain format will be used. 1 — FTOSTR_MODE_ME: Use mantissa/exponent format. 2 — FTOSTR_MODE_PLAIN: Use plain format, not mantissa/exponent representation. |
rnd |
Number of digits to round the result to (total number of non-zero digits in the integer and fractional part of mantissa). |
Details
The ftostr function offers much more control over the format of the output string compared to similar functions found on other systems. For starters, you can select whether you want to see mantissa/exponent, "regular" format, or let the function decide which format to use. Additionally, you control the rounding (i.e., you get to choose how many digits should be displayed) — and this influences the representation both of the fractional and integer part of the value.
Examples below illustrate what you can do with ftostr. This function has a counterpart — fstr — which is invoked implicitly whenever you assign a real to a string (string=real). fstr is just like ftostr, but the mode and rnd parameters are fixed at 0 — FTOSTR_MODE_AUTO and "maximum number of digits possible," respectively.
Examples
dim r1 as real
dim s as string
'demonstrate output formats
r1=10000000000.0 'notice '.0' -- it is necessary or compilier will generate an error
s=ftostr(r1,FTOSTR_MODE_ME,11) 'result will be '1E+010'
s=ftostr(r1,FTOSTR_MODE_PLAIN,11) 'result will be '10000000000'
s=ftostr(r1,FTOSTR_MODE_AUTO,11) 'result will be '1E+010' because this representation is more
compact
'demonstrate rounding
r1=1234567.125
s=ftostr(r1,FTOSTR_MODE_AUTO,15) 'result will be '1234567.125'
s=ftostr(r1,FTOSTR_MODE_AUTO,9) 'result will be '1234567.13'
s=ftostr(r1,FTOSTR_MODE_AUTO,2) 'result will be '1200000'
s=r1 'fstr will be used, result will be '1234567.125'