String.isNumber
func String.isNumber(s: string): boolString.isNumber reports whether every code point of s is a number (N): Nd, Nl such as Ⅻ, or No such as ½. It is false on "".
Details
truewhen every code point ofshas a general category in N (Nd, Nl, No);falseon"".- Besides decimal digits it accepts letter numbers such as
Ⅻand other numbers such as½,², and①. - It tests characters, not syntax: a sign or a decimal point is not a number, and a string that passes need not be one
String.toIntreads.
Basic examples
String.isNumber("42") // true
String.isNumber("½") // true
String.isNumber("4a") // falseCall it through .:
"Ⅻ".isNumber() // trueScope
The empty string has no code points to test, and gives false:
String.isNumber("") // falseDecimal digits of any script are numbers:
String.isNumber("2026") // true
String.isNumber("٣٤") // true
String.isNumber("12") // trueLetter numbers count: Roman numerals, upper and lower case, and the ideographic zero:
String.isNumber("Ⅻ") // true
String.isNumber("ⅻ") // true
String.isNumber("〇") // trueOther numbers count: fractions, superscripts, and circled numbers. The string may mix the three categories:
String.isNumber("¾") // true
String.isNumber("²") // true
String.isNumber("①") // true
String.isNumber("3½") // true
String.isNumber("2²") // trueA Han numeral such as 三 is a letter, Lo, and π is a lower-case letter, so neither is a number:
String.isNumber("三") // false
String.isNumber("π") // falseSigns, points, separators, exponents, and white space are not number characters:
String.isNumber("-1") // false
String.isNumber("1.5") // false
String.isNumber("1_000") // false
String.isNumber("1e3") // false
String.isNumber(" 42") // falseTest one code point at a time with String.chars, here in a pipe:
"x½2" | String.chars() | Array.map({ x.isNumber() }) // [false, true, true]Pass it to a higher-order function as a function value:
["12", "Ⅻ", "", "ab", "¼"].filter(String.isNumber) // ["12", "Ⅻ", "¼"]Applications
Count the number characters in a recipe line:
"Add 1½ cups and 2 eggs".chars().countWhere({ x.isNumber() }) // 3Split an amount from its unit, where the amount may hold a fraction:
type Amount = { qty: string, unit: string }
func amount(s: string): Amount {
let cs = s.chars()
let i = cs.findIndex({ !x.isNumber() }) ?? cs.length()
let qty = String.concat(cs.slice(0, i))
let unit = String.concat(cs.slice(i, cs.length())).trim()
Amount{ qty, unit }
}
for s in ["1½ cups", "250 g", "¾ tsp", "pinch"] {
let a = amount(s)
println("${a.qty}|${a.unit}")
}1½|cups
250|g
¾|tsp
|pinchMark the fields of a line that are numbers of any kind:
for f in "Chapter Ⅻ page 42 note ①".fields() {
println(if f.isNumber() { "[${f}]" } else { f })
}Chapter
[Ⅻ]
page
[42]
note
[①]Properties and relations
String.isNumber is true exactly when the string is not empty and the category of every code point starts with N:
for s in ["", "0", "42", "٣", "½", "Ⅻ", "²", "1.5", "a1", "三"] {
let byCategory = s != "" && s.chars().every({ x.category().startsWith("N") })
assert(String.isNumber(s) == byCategory)
}A decimal digit is a number, so String.isDigit implies String.isNumber, but not the other way around:
for s in ["7", "٣", "½", "Ⅻ", "²", "x"] {
assert(!String.isDigit(s) || String.isNumber(s))
}
String.isDigit("Ⅻ") // false
String.isNumber("Ⅻ") // trueA number character is not a letter, even a Roman numeral that looks like one:
for c in ["7", "Ⅻ", "½", "〇"] {
assert(String.isNumber(c) && !String.isLetter(c))
}
String.isLetter("X") // true
String.isNumber("X") // falsePossible issues
String.isNumber does not mean the string parses as a number. String.toInt reads only the ASCII digits, so a Roman numeral, a fraction, or a digit of another script passes the test and still gives null:
String.isNumber("Ⅻ") // true
String.toInt("Ⅻ") // null
String.toInt("½") // null
String.toInt("٣") // nullTo parse a number, call String.toInt or String.toFloat and check for null instead of testing first.
A number that String.toFloat reads can fail the test: a sign and a decimal point are not number characters:
String.isNumber("-2.5") // false
String.toFloat("-2.5") // -2.5A Roman numeral written with ordinary letters is letters. Only the Roman numeral code points, such as U+216B Ⅻ, are numbers:
String.isNumber("XII") // false
String.isNumber("\u{216B}") // trueA combining mark such as the keycap sign is not a number, so a digit with a mark attached fails the test:
String.isNumber("1\u{20E3}") // false
String.category("\u{20E3}") // "Me"Every code point must be a number, so String.isNumber does not answer whether a string contains one. Use Array.some for that:
String.isNumber("No. 5") // false
"No. 5".chars().some({ x.isNumber() }) // trueSee also
String.isDigit · String.isLetter · String.category · String.toInt · String.toFloat · String.chars · Array.every · Array.some
