Search code examples
vb.netcharroman-numerals

VB.Net - Char.IsNumber input Roman numerals


I just noticed that the Char.IsNumber function accepts Roman numerals (amongst other things) as a number. https://msdn.microsoft.com/en-us/library/y8t708dh%28v=vs.110%29.aspx

But Roman numerals would be strings ("III", "IV") or letters ("V", "X"), how can you input Roman numerals into this method for it to return true?

It makes me wonder what else could be entered that we may not consider to be a number. The number symbol (#) returns false from the Char.IsSymbol method I has discovered for eg.


Solution

  • But Roman numerals would be strings ("III", "IV")…

    In general, you are correct that Roman numbers tend to be strings, not chars.

    However, for certain specific Roman numerals — Ⅰ, Ⅱ, Ⅲ, Ⅳ, …, Ⅻ, Ⅼ, Ⅽ, Ⅾ, Ⅿ, as well as their lower-case versions — Unicode has dedicated (non-surrogate) codepoints. I have just used them! See the following resources for details:

    Note that each of these numerals is a single (non-surrogate) Unicode code point, and as such will fit into just one .NET System.Char.

    …how can you input Roman numerals into this method for it to return true?

    Here is an example in VB.NET:

    Dim romanNumeralFour As Char = ChrW(&H2163)      ' or: "Ⅳ"c
    Assert.IsTrue(Char.IsNumber(romanNumeralFour))
    

    And here is the C# version:

    const char romanNumeralFour = '\u2163';         // or: 'Ⅳ'
    Assert.IsTrue(char.IsNumber(romanNumeralFour));
    

    Like Hans Passant commented above, note that this won't work if you use Latin letters, such as M; the Roman numeral is a different character, even if it might look the same (depending on your display font)!

    It makes me wonder what else could be entered that we may not consider to be a number.

    That method ought to return True for any characters from these Unicode character categories: