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 年前13358 查看次数
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 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则