Home >Database >Mysql Tutorial >How to Perform Accurate Floating-Point Division in SQLite?

How to Perform Accurate Floating-Point Division in SQLite?

Susan Sarandon
Susan SarandonOriginal
2024-12-15 00:17:13607browse

How to Perform Accurate Floating-Point Division in SQLite?

Performing Floating-Point Division in SQLite

When dividing integers in SQLite, the result is returned as an integer. This can be problematic if you want to calculate a real value. To resolve this issue, you can cast one of the numbers to a real number by multiplying it by 1.0.

For example, the following query returns an integer result:

sqlite> select totalUsers/totalBids from 
(select (select count(*) from Bids) as totalBids , 
(select count(*) from Users) as totalUsers) A;
1

To obtain the real value of the division result, multiply one of the numbers by 1.0:

sqlite> select totalUsers*1.0/totalBids from 
(select (select count(*) from Bids) as totalBids , 
(select count(*) from Users) as totalUsers) A;
0.5

The above is the detailed content of How to Perform Accurate Floating-Point Division in SQLite?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn