SQLite 格式编号始终保留 2 个小数位 [英] SQLite format number with 2 decimal places always

查看:22
本文介绍了SQLite 格式编号始终保留 2 个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在包含 FLOAT 列类型的 SQLite 数据库中有一些表,现在我想使用查询检索值并将浮点字段的格式设置为始终保留 2 个小数位,到目前为止我已经编写了这样的查询:

I have some tables in a SQLite database that contains FLOAT column type, now i want to retrieve the value with a Query and format the float field always with 2 decimal places, so far i have written i query like this one :

SELECT ROUND(floatField,2) AD field FROM table

这将返回如下所示的结果集:

this return a resultset like the the following :

3.56
----
2.4
----
4.78
----
3

我想总是用两位小数来格式化结果,所以:

I want to format the result always with two decimal places, so :

3.56
----
2.40
----
4.78
----
3.00

是否可以直接从 SQL 查询中始终使用 2 个小数位进行格式化?如何在 SQLite 中完成?

Is it possible to format always with 2 decimal places directly from the SQL Query ? How can it be done in SQLite ?

谢谢.

推荐答案

您可以使用 printf 为:

You can use printf as:

SELECT printf("%.2f", floatField) AS field FROM table;

例如:

sqlite> SELECT printf("%.2f", floatField) AS field FROM mytable;
3.56
2.40
4.78
3.00
sqlite>

这篇关于SQLite 格式编号始终保留 2 个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆