duanniwu7730 2014-08-28 04:52
浏览 859
已采纳

Go中Unicode中IsDigit和IsNumber之间的区别

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)
}
  • 写回答

2条回答 默认 最新

  • douhuang5623 2014-08-28 05:08
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素