STRF$CONVERSION FunctionWindows


A conversion function that returns a formatted string representation of the value of a numeric expression.

Syntax: STRF$(numeric-expression, Format%, Precision%, Digits%)
A$ = STRF$(99.9934, ffGeneral, 4, 4)
A$ = STRF$(12345678, ffNumber, 8, 0)

Details:
Precision% specifies how many decimal places to calculate.
The following are valid Formats:
0 = ffGeneral      2 = ffFixed1 = ffExponent     3 = ffNumber
ffGeneral converts to the shortest possible decimal string, where trailing zeros are removed. Digits% range from values of 0 to 4.
ffExponent converts to scientific notation of the form -d.ddd...E+dddd. Digits% range from values of 0 to 4.
ffFixed converts to fixed point format of the form -ddd.ddd... Digits% range from values of 0 to 18.
ffNumber converts to a number format of the form -d,ddd,ddd.ddd... Where the resulting string contains thousand separators.


If you want a more simple result (like ffGeneral) that is fast and accurate, use this code:

FUNCTION RoundOff(z AS DOUBLE , accuracy AS INTEGER) AS DOUBLE
    DEFDBL scaleracc = 10^accuracy
    IF z < 0 THEN
        RESULT = CEIL(z * scaleracc - 0.500000#) / scaleracc
    ELSE
        RESULT = FLOOR(z * scaleracc + 0.500000#) / scaleracc
    END IF
END FUNCTION


FUNCTION STRFX$(TheVal AS DOUBLE, accuracy AS INTEGER) AS STRING
    RESULT = STR$(RoundOff(TheVal, accuracy))
END FUNCTION