SQLite format number with 2 decimal places always

后端 未结 4 1176
日久生厌
日久生厌 2020-12-08 02:38

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 place

相关标签:
4条回答
  • 2020-12-08 03:29

    You could try Select Str(12345.6789, 12, 3)

    displays: ' 12345.679'

    ( 3 spaces, 5 digits 12345, a decimal point, and three decimal digits (679). - it rounds if it has to truncate, (unless the integer part is too large for the total size, in which case asterisks are displayed instead.)

    for a Total of 12 characters, with 3 to the right of decimal point.

    0 讨论(0)
  • Due to the way sqlite stores numbers internally,

    You probably want something like this

    select case when substr(num,length(ROUND(floatField,2))-1,1) = "." then num || "0" else num end from table;
    
    0 讨论(0)
  • 2020-12-08 03:34

    On older versions of SQLite that don't have PRINTF available you can use ROUND.

    For example...

    SELECT 
       ROUND(time_in_secs/1000.0/60.0, 2) || " mins" AS time,
       ROUND(thing_count/100.0, 2) || " %" AS percent
    FROM
       blah
    
    0 讨论(0)
  • 2020-12-08 03:35

    You can use printf as:

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

    for example:

    sqlite> SELECT printf("%.2f", floatField) AS field FROM mytable;
    3.56
    2.40
    4.78
    3.00
    sqlite>
    
    0 讨论(0)
提交回复
热议问题