R中数字的逗号分隔符
在本文中,我们将看到在 R 编程语言中使用逗号分隔数字。数字中的逗号分隔符将使数字清晰,当数字用逗号分隔时,我们可以轻松计数。
可以通过以下方式完成:
- 使用prettyNum()
- 使用格式()
方法一:使用prettyNum()
该函数用于分隔数字。
Syntax: prettyNum(input_number, big.mark = “,”, scientific = FALSE)
Parameters:
- input_number – input number
- big mark- it is an separator such that the number should be separated by the given delimiter.
- scientific parameter – It is set to FALSE.
Return: Number separated by comma
示例 1: R 程序用逗号分隔数字。
R
# input number
x = 70589999999
# number sepaarted with comma with
# prettyNum
prettyNum(x, big.mark = ",", scientific = FALSE)
R
# input number
x = 45000000000000000000
# number sepaarted with comma with
# prettyNum scientific = FALSE
prettyNum(x, big.mark = ",", scientific = FALSE)
# number sepaarted with comma with
# prettyNum with scientific = TRUE
prettyNum(x, big.mark = ",", scientific = TRUE)
R
# input number
x = 70589999999
# number sepaarted with comma with format
prettyNum(x, big.mark = ",", scientific = FALSE)
R
# input number
x = 720589999999
# number separated with comma with
# format scientific = FALSE
format(x, big.mark = ",", scientific = FALSE)
# number separated with comma with
# format with scientific = TRUE
format(x, big.mark = ",", scientific = TRUE)
输出:
'70,589,999,999'
示例 2: R 程序用逗号分隔数字与科学值
电阻
# input number
x = 45000000000000000000
# number sepaarted with comma with
# prettyNum scientific = FALSE
prettyNum(x, big.mark = ",", scientific = FALSE)
# number sepaarted with comma with
# prettyNum with scientific = TRUE
prettyNum(x, big.mark = ",", scientific = TRUE)
输出:
'45,000,000,000,000,000,000'
'4.5e+19'
注意:当科学设置为 FALSE 时,我们不会得到逗号分隔的数字
方法二:使用format()
用于用分隔符格式化数字
Syntax: format(input_number, big.mark = “,”, scientific = FALSE)
Parameters:
- input_number – input number
- big mark- it is an separator such that the number should be separated by the given delimiter.
- scientific parameter – It is set to FALSE.
Return: Number separated by comma.
示例 1: R 程序用逗号分隔数字
电阻
# input number
x = 70589999999
# number sepaarted with comma with format
prettyNum(x, big.mark = ",", scientific = FALSE)
输出:
'70,589,999,999'
示例 2: R 程序用逗号分隔数字与科学值
电阻
# input number
x = 720589999999
# number separated with comma with
# format scientific = FALSE
format(x, big.mark = ",", scientific = FALSE)
# number separated with comma with
# format with scientific = TRUE
format(x, big.mark = ",", scientific = TRUE)
输出:
'720,589,999,999'
'7.2059e+11'