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

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码