FloatToString (Float function): Difference between revisions
m (1 revision) |
m (1 revision) |
(No difference)
|
Revision as of 14:18, 19 January 2011
Convert a number to a string (Float class)
This intrinsic function converts a floating point number to a string with a specific
length and number of decimal places.
The FloatToString function is available as of version 7.6 of the Sirius Mods.
Syntax
%string = float:FloatToString[( [Length= number], [DP= number])]
Syntax terms
%str | A string variable to receive the converted value of the method object number. |
---|---|
[Length=]len | This optional, name-required, argument (Length) specifies the output string length. The len value must be between 1 and 255, and it defaults to the minimum length required to represent the number. |
[Dp=]num | This optional, name-required, argument (Dp) specifies the number of decimal places in the result. The num value defaults to 0, must be between 0 and 75, and must be less than the Length parameter (if a Length value is specified). |
Usage notes
If the target length and number of decimal places would result in leading non-zero digits being lost, the request is canceled. For example, specifying the following results in request cancellation:
13.1415926:floatToString(length=6, dp=4)
FloatToString neatly displays non-integer numeric values. The Round method (:hdref refid=flround.) is not a good way of doing this, since Round returns a Float value and strips trailing 0s when printing the value.
FloatToString currently does not support E format (9.9E72) output.
A 'Dp' value of 0 has a special result: no decimal point. There is no way to get a trailing decimal point without any digits after it unless you explicitly append a point character ('.') to a result for which 'Dp=0' is specified, as is shown in the last of the PrintText statements in the example below, you can get a value with a leading decimal point and no digit before it.
To produce the specified length or number of decimal places in the result, FloatToString uses truncation, not rounding, of the input, as you can see in the last two of the PrintText statements in the example below.
Examples
The following program demonstrates the FloatToString method.
begin printText {~} = {0.1415926:floatToString} printText {~} = {0.1415926:floatToString(dp=0)} printText {~} = {0.1415926:floatToString(dp=1)} printText {~} = {0.1415926:floatToString(dp=5)} printText {~} = {0.1415926:floatToString(length=10)} printText {~} = {0.1415926:floatToString(length=10, dp=1)} printText {~} = {0.1415926:floatToString(length=10, dp=4)} printText {~} = {0.1415926:floatToString(length=5, dp=4)} end
The result is:
0.1415926:floatToString = 0 0.1415926:floatToString(dp=0) = 0 0.1415926:floatToString(dp=1) = 0.1 0.1415926:floatToString(dp=5) = 0.14159 0.1415926:floatToString(length=10) = 0 0.1415926:floatToString(length=10, dp=1) = 0.1 0.1415926:floatToString(length=10, dp=4) = 0.1415 0.1415926:floatToString(length=5, dp=4) = .1415
See also
List of intrinsic Float methods