Core\Db\Row::decimal
Reads one column of a row as an exact number.
Core\Db\Row::decimal takes the name of a column and returns a decimal. That is a number with up to 29 digits, and it keeps every one of them. It is the type for money. The columns it reads are DECIMAL, NUMERIC and MONEY.
The result is null when the column has no value in this row, so the type you get back is ?decimal. Test it for null before you use it.
The method throws a LogicError when the row has no column with that name, and when the column is not an exact numeric one. A FLOAT, REAL or DOUBLE column is read with Core\Db\Row::float, and that method reads no DECIMAL column. A price that passes through a float can come back a hundredth away from the value that was stored.
The examples below print the price of a product, read a column that may have no value, and add up what an order costs.
Parameters
$namestringThe column label, as the server described it.
Return value
Returns ?decimal. The exact number, or null for a NULL column.
Errors
LogicErrorThe row has no column with that name, or the column is not an exact numeric one.
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 price out of a row
<?nvs// Shows how to read an exact number out of a row.
var $db = Core\Db::connect('notes');$db->execute('create table products (name text not null, price decimal(10,2) not null)', []);decimal $tea = 4.25;decimal $pot = 19.95;$db->execute('insert into products (name, price) values (?, ?), (?, ?)', ['tea', $tea, 'teapot', $pot]);
// `decimal` reads the named column as an exact number. This prints `tea: 4.25`// and `teapot: 19.95`.foreach ($db->query('select name, price from products order by name', []) as Core\Db\Row $row) { ?decimal $price = $row->decimal('price'); if ($price != null) { echo $row->string('name') ?? '', ': ', $price, "\n"; }}
$db->close();tea: 4.25teapot: 19.95A column that has no value
<?nvs// Shows what you get when the number column has no value in this row.
var $db = Core\Db::connect('notes');$db->execute('create table products (name text not null, price decimal(10,2))', []);decimal $pot = 19.95;$db->execute('insert into products (name, price) values (?, ?), (?, ?)', ['sample', null, 'teapot', $pot]);
// `decimal` returns `null` for a column with no value, so your program says what// to print instead. This prints `sample: no price` and the price of the other row.foreach ($db->query('select name, price from products order by name', []) as Core\Db\Row $row) { ?decimal $price = $row->decimal('price'); echo $row->string('name') ?? '', ': '; if ($price != null) { echo $price, "\n"; } else { echo "no price\n"; }}
$db->close();sample: no priceteapot: 19.95Add up what an order costs
<?nvs// Shows how to add up the lines of an order. Every line is read as a `decimal`,// so the total keeps every cent of every line.
var $db = Core\Db::connect('notes');$db->execute('create table order_lines (item text not null, amount decimal(10,2) not null)', []);decimal $tea = 4.25;decimal $pot = 19.95;decimal $caddy = 31.05;$db->execute( 'insert into order_lines (item, amount) values (?, ?), (?, ?), (?, ?)', ['tea', $tea, 'teapot', $pot, 'caddy', $caddy],);
decimal $total = 0.00;foreach ($db->query('select amount from order_lines', []) as Core\Db\Row $row) { ?decimal $amount = $row->decimal('amount'); if ($amount != null) { $total = $total + $amount; }}
// This prints `total: 55.25`.echo 'total: ', $total, "\n";
$db->close();total: 55.25