How to use bigquery round up results to 4 digits after decimal point?

独自空忆成欢 提交于 2020-06-23 06:29:30

问题


We don't have decimal data type in BigQuery now. So I have to use float

But

In Bigquery float division

0.029*50/100=0.014500000000000002

Although

0.021*50/100=0.0105

To round the value up

I have to use round(floatvalue*10000)/10000.

Is this the right way to deal with decimal data type now in BigQuery?


回答1:


Note that this question deserves a different answer now.

The premise of the question is "We don't have decimal data type in BigQuery now."

But now we do: You can use NUMERIC:

SELECT CAST('0.029' AS NUMERIC)*50/100

# 0.0145

Just make your column is NUMERIC instead of FLOAT64, and you'll get the desired results.




回答2:


Depends on your coding preferences - for example you can just use simple ROUND(floatvalue, 4)
Depends on how exactly you need to round - up or down - you can respectively adjust expression
For example ROUND(floatvalue + 0.00005, 4)

See all rounding functions for BigQuery Standard SQL at below link

https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#rounding-functions



来源:https://stackoverflow.com/questions/43006002/how-to-use-bigquery-round-up-results-to-4-digits-after-decimal-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!