
Last chance! 50% off unlimited learning
Sale ends in
Cast numbers of any kind including decimals.
types.castNumber(
format,
value,
options = {
}
)
no options (other than the default)
number to cast
available options are "decimalChar", "groupChar" and "bareNumber", where
decimalChar
A string whose value is used to represent a decimal point within the number. The default value is ".".
groupChar
A string whose value is used to group digits within the number. The default value is null. A common value is "," e.g. "100,000".
bareNumber
A boolean field with a default of TRUE
If TRUE
the physical contents of this field must follow
the formatting constraints already set out. If FALSE
the contents of this field may contain leading and/or
trailing non-numeric characters (which implementors MUST therefore strip). The purpose of bareNumber
is to allow publishers
to publish numeric data that contains trailing characters such as percentages e.g. 95
e.g. €95 or EUR 95. Note that it is entirely up to implementors what, if anything, they do with stripped text.
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)
types.castNumber(format = "default", value = "1.0")
# cast number with percent sign
types.castNumber(format = "default", value = "10.5%", options = list(bareNumber = FALSE))
# cast number with comma group character
types.castNumber(format = "default", value = "1,000", options = list(groupChar = ','))
types.castNumber(format = "default", value = "10,000.50", options = list(groupChar = ','))
# cast number with "#" group character and "&" as decimal character
types.castNumber(format = "default", value = "10#000&50",
options = list(groupChar = '#', decimalChar = '&'))
Run the code above in your browser using DataLab