英文:
Differences between IsDigit and IsNumber in unicode in Go
问题
似乎在unicode包中,IsDigit和IsNumber函数的行为没有区别,至少在我下面的测试代码中是这样的:
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报告给定的符文是否为数字(类别N)。
func IsNumber(r rune) bool {
if uint32(r) <= MaxLatin1 {
return properties[uint8(r)]&pN != 0
}
return isExcludingLatin(Number, r)
}
// IsDigit报告给定的符文是否为十进制数字。
func IsDigit(r rune) bool {
if r <= MaxLatin1 {
return '0' <= r && r <= '9'
}
return isExcludingLatin(Digit, r)
}
以上是它们的源代码。
英文:
It seems IsDigit and IsNumber in the unicode package don't 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 don't 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)
}
答案1
得分: 27
一般类别是数字,子类别是十进制数字。
Unicode标准中的相关信息如下:
4.5 一般类别
Nd = 数字,十进制数字
Nl = 数字,字母
No = 数字,其他
4.6 数值
数值和数值类型是表示数字的字符的规范属性。
十进制数字。
十进制数字通常是用于构成十进制数的数字。
例如,
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
英文:
The general category is number and the sub category is decimal digit.
> Unicode Standard
>
> 4. Character Properties
>
>
> 4.5 General Category
>
> Nd = Number, decimal digit
> Nl = Number, letter
> No = Number, other
>
> 4.6 Numeric Value
>
> Numeric_Value and Numeric_Type are normative properties of characters
> that represent numbers.
>
> Decimal Digits.
>
> Decimal digits, as commonly understood, are digits used to form
> decimal-radix numbers.
For example,
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))
}
Output:
true
true
false
true
false
true
答案2
得分: 5
据我所知,IsDigit()
是IsNumber()
的一个子集,所以你得到的结果是正确的,因为两者都应该评估为true
。IsNumber
用于确定字符是否属于任何数字类别,而IsDigit()
则检查字符是否为十进制数字。
英文:
As far as I know IsDigit()
is a subset of IsNumber()
so the result which you are getting is fine since both should evaluate to true
. The IsNumber
is use to determine if it is in any numeric Unicode category and IsDigit()
checks if it is a radix-10 digit..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论