Skip to main content

string.tofloat()

Converts a string of numeric characters to a float value

Availability

Device + Agent

Returns

Float — the value of the numeric string

Description

This method returns the float value represented by the target string, which must contain only numeric characters, and the minus and plus symbols to indicate negative and positive values, respectively. The presence of other characters will cause Squirrel to post a ‘cannot convert the string’ error.

To trap cases where the string is not numeric, use Squirrel’s try... catch structure:

local aString = "Zap!";
local value = -1.0;

try {
    local = aString.tofloat();
catch (err) {
    server.error("Integer conversion attempted on non-numeric string; defaulting to 0.0");
    local = 0.0;
}

server.log(local);
// Logs '0.0'

Example Code