Cast numbers of any kind including decimals.

types.castNumber(format, value, options = {
})

Arguments

format

no options (other than the default)

value

number to cast

options

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.

Details

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)

See also

Examples

types.castNumber(format = "default", value = 1)
#> [1] 1
types.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
types.castNumber(format = "default", value = "10,000.50", options = list(groupChar = ','))
#> [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