Differences between IsDigit and IsNumber in Unicode in Go

It seems that IsDigit and IsNumber in the unicode package do not behave differently, at least in my following test code:

package main import "fmt" import "unicode" func main() { r := rune('1') fmt.Println(unicode.IsDigit(r)) fmt.Println(unicode.IsNumber(r)) //true //true } 

They both print true .

I tried to understand from their source code. However, I still do not understand what the differences are between them, even from their source code.

 // IsNumber reports whether the rune is a number (category N). func IsNumber(r rune) bool { if uint32(r) <= MaxLatin1 { return properties[uint8(r)]&pN != 0 } return isExcludingLatin(Number, r) } // IsDigit reports whether the rune is a decimal digit. func IsDigit(r rune) bool { if r <= MaxLatin1 { return '0' <= r && r <= '9' } return isExcludingLatin(Digit, r) } 
+5
source share
2 answers

The general category is a number, and the subcategory is a decimal digit.

Unicode Standard

4. Character properties

4.5 General category

 Nd = Number, decimal digit Nl = Number, letter No = Number, other 

4.6 Numerical value

Numeric_Value and Numeric_Type are normative character properties that represent numbers.

Decimal digits.

Decimal digits are commonly understood to be the numbers used to form decimal numbers.

For instance,

Unicode Characters in the Number, Decimal Digit (Nd) Category

Unicode Symbols in the Number, Letter Category (Nl)

Unicode characters in the Number, Other category (none)

 package main import ( "fmt" "unicode" ) func main() { digit := rune('1') fmt.Println(unicode.IsDigit(digit)) fmt.Println(unicode.IsNumber(digit)) letter := rune('β…§') fmt.Println(unicode.IsDigit(letter)) fmt.Println(unicode.IsNumber(letter)) other := rune('Β½') fmt.Println(unicode.IsDigit(other)) fmt.Println(unicode.IsNumber(other)) } 

Conclusion:

 true true false true false true 
+13
source

As far as I know, IsDigit() is a subset of IsNumber() , so the result you get is great, since both should be evaluated to true . IsNumber used to determine if it is in any Unicode number category, and IsDigit() checks to see if it is a radix-10 digit.

+4
source

Source: https://habr.com/ru/post/1201296/


All Articles