Core\Db\Row::float
Reads one column of a row as a floating-point number.
Core\Db\Row::float takes the name of a column and returns the number in it. It reads the floating-point column types: REAL, FLOAT and DOUBLE PRECISION. These columns store a number in binary. A value like 0.1 has no exact binary form, so the column keeps the closest binary number to it.
The result is null when the column has no value in this row, so the type you get back is ?float. Test it for null, or write ?? 0.0 after the call to use zero instead.
The method throws a LogicError when the row has no column with that name, and when the column is not a floating-point column. A DECIMAL column holds an exact number, and reading it in binary would change the value, so that column is read with Core\Db\Row::decimal. A whole-number column is read with Core\Db\Row::int or Core\Db\Row::uint.
The examples below read a temperature, read a column that may have no value, and pick out the stops inside a map area.
Parameters
$namestringThe column label, as the server described it.
Return value
Returns ?float. The number, or null for a NULL column.
Errors
LogicErrorThe row has no column with that name, or the column is not a floating-point column. ADECIMALis exact, so read it with->decimal. Binary floating point gives wrong sums for money.
Examples
The examples below run with this configuration file.
# The `notes` database is kept in memory. It is deleted when the program ends.[db.notes]driver = "sqlite"path = ":memory:"
# The program may open the `notes` database.[capabilities.db]connect = ["notes"]Read a measurement out of a row
<?nvs// Shows how to read a measurement out of a row.
var $db = Core\Db::connect('notes');$db->execute('create table readings (station text not null, celsius real not null)', []);$db->execute( 'insert into readings (station, celsius) values (?, ?), (?, ?)', ['north field', 21.5, 'south field', 9.25],);
// `float` reads the named column as a floating-point number. This prints one// line for each station.foreach ($db->query('select station, celsius from readings order by station', []) as Core\Db\Row $row) { echo $row->string('station') ?? '', ': ', $row->float('celsius') ?? 0.0, " degrees\n";}
$db->close();north field: 21.5 degreessouth field: 9.25 degreesA column that has no value
<?nvs// Shows what you get when the column has no value in this row.
var $db = Core\Db::connect('notes');$db->execute('create table readings (station text not null, celsius real)', []);$db->execute( 'insert into readings (station, celsius) values (?, ?), (?, ?)', ['north field', 21.5, 'south field', null],);
// The result is `null` when the column has no value in this row. The type you// get back is `?float`, so you test it for `null` before you use it.foreach ($db->query('select station, celsius from readings order by station', []) as Core\Db\Row $row) { ?float $celsius = $row->float('celsius'); if ($celsius == null) { echo $row->string('station') ?? '', ': the station sent no reading', "\n"; } else { echo $row->string('station') ?? '', ': ', $celsius, " degrees\n"; }}
$db->close();north field: 21.5 degreessouth field: the station sent no readingPick the stops inside a map area
<?nvs// Shows how to use two floating-point columns of the same row. This prints the// stops that are inside the area a map shows.
var $db = Core\Db::connect('notes');$db->execute('create table stops (name text not null, lat real not null, lon real not null)', []);$db->execute( 'insert into stops (name, lat, lon) values (?, ?, ?), (?, ?, ?), (?, ?, ?)', ['main square', 47.0707, 15.4395, 'river bridge', 47.0833, 15.4167, 'south gate', 47.0512, 15.4389],);
// The corners of the area. A stop is inside it when both of its numbers are// between the two corners.float $north = 47.08;float $south = 47.06;float $west = 15.43;float $east = 15.45;
foreach ($db->query('select name, lat, lon from stops order by name', []) as Core\Db\Row $row) { float $lat = $row->float('lat') ?? 0.0; float $lon = $row->float('lon') ?? 0.0; if ($lat <= $north && $lat >= $south && $lon >= $west && $lon <= $east) { echo $row->string('name') ?? '', " is on the map\n"; } else { echo $row->string('name') ?? '', " is outside the map\n"; }}
$db->close();main square is on the mapriver bridge is outside the mapsouth gate is outside the map