2014-08-28 18 views
5

看來ISDIGIT和ISNUMBER在Unicode包不表現不同,至少在我的下面的測試代碼:的差異unicode的圍棋

package main 

import "fmt" 
import "unicode" 

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

他們都打印true

我試圖從他們的源代碼中瞭解。但是,我仍然不明白它們之間的差異,甚至不知道它們的源代碼。

// 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) 
} 

回答

13

一般類別是數字,子類別是十進制數字。

Unicode Standard

4. Character Properties

4.5普通類

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

4.6數字值

Numeric_Value和Numeric_Type是代表數字字符 的規範性。

十進制數字。

小數位,正如通常所理解的,是用於形成小數位數的 的數字。

例如,

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

Unicode Characters in the 'Number, Letter' Category (Nl)

Unicode Characters in the 'Number, Other' Category (No)

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)) 
} 

輸出:

true 
true 
false 
true 
false 
true 
4

據我所知IsDigit()IsNumber()一個子集這樣,你所得到的結果是好的,因爲兩者都應該評估爲trueIsNumber用於確定它是否在任何數字Unicode類別中,並且IsDigit()檢查它是否爲基數爲10的數字。