How can I format a decimal (DOUBLE) number in a select statement?

0

Hi, I have the following select statement in Athena and I would like to set the number of digits right to the decimal period of the column value:

SELECT
,(CAST(column1 AS double) / column2) cvr
FROM
  my_table

For example, I current get the following result: 0.6453524

I would like to get it as 0.645

已提問 2 年前檢視次數 13353 次
3 個答案
1

Hi, it depends on what specifically do you want, with a similar example 0.6456524 and 3 decimals

  • If you want to have a ceiling of the previous decimal value : 0.646 To do this you can use a UDF or this mathematic expression CEIL((CAST(column1 AS double) / column2)*1000)/1000 as cvr
  • if you want to have a floor of the previous decimal value : 0.645 FLOOR((CAST(column1 AS double) / column2)*1000)/1000 as cvr
  • if you want to numerically round the previous decimal value : 0.646. To do this you can use ROUND((CAST(column1 AS double) / column2), 3) as cvr You can always refer to the Athena operations documentation here
AWS
已回答 2 年前
AWS
專家
已審閱 2 年前
0

Suggest to use the round function. See here

profile pictureAWS
專家
Roi
已回答 2 年前
AWS
專家
已審閱 2 年前
0

You can use ROUND(col, 3) to round a number to three decimal points:

SELECT ROUND(CAST(column1 AS DOUBLE) / column2), 3) AS cvr
FROM my_table
AWS
Theo
已回答 2 年前
AWS
專家
已審閱 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南