In SQLite, division operations typically return integer values, even when working with decimal numbers. This can be problematic if you want to obtain precise results. To address this issue, it's possible to coerce the result to a real value.
One method to achieve this conversion is to multiply one of the numbers by 1.0, as demonstrated below:
SELECT something*1.0/total FROM somewhere
By multiplying one of the numbers by 1.0, you enforce floating-point division instead of integer division. This ensures that the result is a precise decimal value, as illustrated in the following example:
sqlite> select totalUsers*1.0/totalBids from (select (select count(*) from Bids) as totalBids , (select count(*) from Users) as totalUsers) A; 1.0
This approach provides an easy way to obtain real-valued division results in SQLite, allowing you to perform accurate calculations on decimal values.
The above is the detailed content of How to Get Accurate Floating-Point Division Results in SQLite?. For more information, please follow other related articles on the PHP Chinese website!