ToFloat (open)

Creates a FLOAT value from a string.

Syntax

LOADLIB "wh::float.whlib";

FLOAT FUNCTION ToFloat(STRING value, FLOAT defval)

Parameters

STRING value

The STRING value to read the FLOAT value from

FLOAT defval

The FLOAT value to return when the string could not be converted

Return value

FLOAT

The FLOAT value read from @italic value

Description

This function creates a FLOAT value from a string. The function automatically determines the decimal separator by searching the string for a '.' or ',' decimal separator. When no separator is found, the decimal point is assumed to be at the end of the string. When the string could not be converted, the default value @italic defval is returned.

Examples

// The FLOAT value f will be: 1345.4
FLOAT f := ToFloat("1345,4", 0);

// The FLOAT value f will be: 1.5e-6
FLOAT f := ToFloat(".0000015", 0);

// The FLOAT value f will be: 0.0 (duplicate decimal separator ',')
FLOAT f := ToFloat("1,234,567", 0);

// The FLOAT value f will be: 0.0 ("e" is not valid in float strings)
FLOAT f := ToFloat("1.5e-6", 0);