Comma separator for numbers in R
Last Updated :
28 Dec, 2021
In this article, we are going to see the separation of numbers using a comma in R Programming Language. A comma separator in number will give clarity of a number, and we can count easily when a number is separated by a comma.
It can be done with these ways:
- Using prettyNum()
- Using format()
Method 1: Using prettyNum()
This function is used to separate the number.
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
Example 1: R program to separate the number with a comma.
R
x = 70589999999
prettyNum (x, big.mark = "," , scientific = FALSE )
|
Output:
'70,589,999,999'
Example 2: R program to separate the number with a comma with scientific values
R
x = 45000000000000000000
prettyNum (x, big.mark = "," , scientific = FALSE )
prettyNum (x, big.mark = "," , scientific = TRUE )
|
Output:
'45,000,000,000,000,000,000'
'4.5e+19'
Note: we will not get the comma-separated number when scientific is set to FALSE
Method 2: Using the format()
Used to format the number with a separator
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.
Example 1: R program to separate the number with comma
R
x = 70589999999
prettyNum (x, big.mark = "," , scientific = FALSE )
|
Output:
'70,589,999,999'
Example 2: R program to separate the number with a comma with scientific values
R
x = 720589999999
format (x, big.mark = "," , scientific = FALSE )
format (x, big.mark = "," , scientific = TRUE )
|
Output:
'720,589,999,999'
'7.2059e+11'
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...