/
number_format.Rd
146 lines (117 loc) · 4.43 KB
/
number_format.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/formatter.r
\name{number_format}
\alias{number_format}
\alias{number}
\alias{comma_format}
\alias{comma}
\alias{percent_format}
\alias{percent}
\alias{unit_format}
\alias{degree_format}
\alias{number_si}
\title{Number formatters}
\usage{
number_format(accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...)
number(x, accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...)
comma_format(accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = ",", decimal.mark = ".", trim = TRUE, digits, ...)
comma(x, accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = ",", decimal.mark = ".", trim = TRUE, digits, ...)
percent_format(accuracy = NULL, scale = 100, prefix = "",
suffix = "\%", big.mark = " ", decimal.mark = ".", trim = TRUE,
...)
percent(x, accuracy = NULL, scale = 100, prefix = "",
suffix = "\%", big.mark = " ", decimal.mark = ".", trim = TRUE,
...)
unit_format(accuracy = 1, scale = 1, prefix = "", unit = "m",
sep = " ", suffix = paste0(sep, unit), big.mark = " ",
decimal.mark = ".", trim = TRUE, ...)
degree_format(accuracy = 1, scale = 1, prefix = "", unit = "",
sep = "", suffix = paste0(sep, "°", unit), big.mark = " ",
decimal.mark = ".", trim = TRUE, ...)
number_si(x, prefix = "", suffix = "", ...)
}
\arguments{
\item{accuracy}{Number to round to, \code{NULL} for automatic guess.}
\item{scale}{A scaling factor: \code{x} will be multiply by \code{scale} before
formating (useful if the underlying data is on another scale,
e.g. for computing percentages or thousands).}
\item{prefix, suffix}{Symbols to display before and after value.}
\item{big.mark}{Character used between every 3 digits to separate thousands.}
\item{decimal.mark}{The character to be used to indicate the numeric
decimal point.}
\item{trim}{Logical, if \code{FALSE}, values are right-justified to a common
width (see \code{\link[base:format]{base::format()}}).}
\item{...}{Other arguments passed on to \code{\link[base:format]{base::format()}}.}
\item{x}{A numeric vector to format.}
\item{digits}{Deprecated, use \code{accuracy} instead.}
\item{unit}{The units to append.}
\item{sep}{The separator between the number and the unit label.}
}
\value{
\code{*_format()} returns a function with single parameter
\code{x}, a numeric vector, that returns a character vector.
}
\description{
A set of functions to format numeric values:
\itemize{
\item \code{number_format()} and \code{number()} are generic formatters for numbers.
\item \code{comma_format()} and \code{comma()} format numbers with commas separating
thousands.
\item \code{percent_format()} and \code{percent()} multiply values by one hundred and
display percent sign.
\item \code{unit_format()} adds units to the values.
\item \code{number_si()} scales and adds abbreviated SI units to the values.
}
All formatters allow you to re-\code{scale} (multiplicatively), to round to
specified \code{accuracy}, to add custom \code{suffix} and \code{prefix} and to specify
\code{decimal.mark} and \code{big.mark}.
\code{number_si()} gives limited SI unit labels: "K" for values \eqn{\ge} 10e3, "
M" for \eqn{\ge} 10e6, "B" for \eqn{\ge} 10e9, and "T" for \eqn{\ge} 10e12.
It respects all arguments except \code{scale} which is set internally.
}
\examples{
# number()
v <- c(12.3, 4, 12345.789, 0.0002)
number(v)
number(v, big.mark = ",")
number(v, accuracy = .001)
number(v, accuracy = .001, decimal.mark = ",")
number(v, accuracy = .5)
# number_format()
my_format <- number_format(big.mark = "'", decimal.mark = ",")
my_format(v)
# comma() and comma_format()
comma_format()(c(1, 1e3, 2000, 1e6))
comma_format(accuracy = .01)(c(1, 1e3, 2000, 1e6))
comma(c(1, 1e3, 2000, 1e6))
# percent() and percent_format()
percent_format()(runif(10))
percent(runif(10))
per_mille <- percent_format(
scale = 1000,
suffix = "\\u2030",
accuracy = .1
)
per_mille(.1234)
french_percent <- percent_format(
decimal.mark = ",",
suffix = " \%"
)
french_percent(runif(10))
# unit_format()
# labels in kilometer when the raw data are in meter
km <- unit_format(unit = "km", scale = 1e-3, digits = 2)
km(runif(10) * 1e3)
# labels in hectares, raw data in square meters
ha <- unit_format(unit = "ha", scale = 1e-4)
km(runif(10) * 1e5)
# degree_format()
celcius <- degree_format(unit = "C")
celcius(rnorm(10) * 10)
west <- degree_format(unit = "W")
west(runif(10) * 100)
}