Cast numbers of any kind including decimals.
types.castNumber(format, value, options = { })
format | no options (other than the default) |
---|---|
value | number to cast |
options | available options are "decimalChar", "groupChar" and "bareNumber", where
|
The lexical formatting follows that of decimal in XMLSchema: a non-empty finite-length sequence of decimal digits separated by a period as a decimal indicator. An optional leading sign is allowed. If the sign is omitted, "+" is assumed. Leading and trailing zeroes are optional. If the fractional part is zero, the period and following zero(es) can be omitted. For example: '-1.23', '12678967.543233', '+100000.00', '210'.
The following special string values are permitted (case need not be respected):
NaN
: not a number
INF
: positive infinity
-INF
: negative infinity
A number MAY also have a trailing:
exponent
: this MUST
consist of an E followed by an optional + or - sign followed by one or more decimal digits (0-9)
types.castNumber(format = "default", value = 1)#> [1] 1types.castNumber(format = "default", value = "1.0")#> [1] 1# cast number with percent sign types.castNumber(format = "default", value = "10.5%", options = list(bareNumber = FALSE))#> [1] 10.5# cast number with comma group character types.castNumber(format = "default", value = "1,000", options = list(groupChar = ','))#> [1] 1000#> [1] 10000.5# cast number with "#" group character and "&" as decimal character types.castNumber(format = "default", value = "10#000&50", options = list(groupChar = '#', decimalChar = '&'))#> [1] 10000.5