简体   繁体   English

如何在 BigQuery 中将数值转换为货币?

[英]How to turn numeric value into currency in BigQuery?

I am new to BigQuery and am trying to convert numeric values (from Salesforce) to currency (preferably dollar value).我是 BigQuery 的新手,正在尝试将数值(来自 Salesforce)转换为货币(最好是美元值)。

Very basically, what I have currently is:基本上,我目前拥有的是:

SELECT salesforce.Name,
       ROUND(salesforce.Amount,2) as Amount 

FROM table.salesforce

Which obviously only rounds the value to two decimal places.这显然只将值四舍五入到小数点后两位。

Regarding your question about how to convert a numeric value to currency value in BigQuery, I would advise you to use the FORMAT() and CONCAT() built-in functions.关于如何在 BigQuery 中将数值转换为货币值的问题,我建议您使用FORMAT()CONCAT()内置函数。

I see that in your question you mentioned you want to round the numeric values to the second decimal place, you can do that using FORMAT() , you can read more about it here .我看到在你的问题中你提到你想要将数值四舍五入到小数点后第二位,你可以使用FORMAT()来做到这一点,你可以在这里阅读更多关于它的信息 In addition, to use the "$" sign, you can use CONCAT() .此外,要使用“$”符号,您可以使用CONCAT() Below is an example where I used some dummy data to exemplify what I explained:下面是一个示例,我使用了一些虚拟数据来举例说明我的解释:

  WITH
  data AS (
  SELECT
    20.21 AS num
  UNION ALL
  SELECT
    99999999.12 AS num
  UNION ALL
  SELECT
    12345 AS num )
  SELECT
  CONCAT('$ ',FORMAT("%'.2f", num)) AS new_num
  FROM
  data

And the output:和输出:

在此处输入图片说明

Notice that in the FORMAT() function I used "%'.2f" , which rounds the number to the second decimal place.请注意,在FORMAT()函数中,我使用了"%'.2f" ,它将数字四舍五入到小数点后第二位。 You can find more information about the meaning of each letter/number in the expression using the following guide .您可以使用以下指南找到有关表达式中每个字母/数字含义的更多信息。

As a bonus information, the currency values are formatted in a way that the dot "."作为奖励信息,货币值的格式为点“.”。 is a decimal separator and the comma "," is a grouping separator.是小数点分隔符,逗号“,”是分组分隔符。 You can switch that using regex expressions with REGEX_REPLACE() and REPLACE() functions.您可以使用带有REGEX_REPLACE()REPLACE()函数的正则表达式来切换它。 If that is the case, just let me know so I can help.如果是这种情况,请告诉我,以便我可以提供帮助。

This is the method that I use:这是我使用的方法:

CAST(YourNumber AS STRING FORMAT '$999,999')

With decimal points:带小数点:

CAST(YourNumber AS STRING FORMAT '$999,999.00')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM