Checking for a decimal digit

J

Jay

I want to check if a character is a decimal digit (0-9).

I've found Char.IsNumber() and Char.IsDigit() but couldn't work out from Help how they differ.

Also, do I need to worry about how digits get converted when typed into keyboards in different
countries? Can I assume that Char.IsNumber() and Char.IsDigit() cope with this?
 
J

Jon Skeet [C# MVP]

I want to check if a character is a decimal digit (0-9).

I've found Char.IsNumber() and Char.IsDigit() but couldn't work out
from Help how they differ.

From the docs for IsNumber:

<quote>
This method determines if a Char is of any numeric Unicode category.
This contrasts with IsDigit, which determines if a Char is a radix-10
digit.

Valid numbers are members of the following categories in
UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
Also, do I need to worry about how digits get converted when typed
into keyboards in different countries? Can I assume that
Char.IsNumber() and Char.IsDigit() cope with this?

The input method is irrelevant to the value when you've got it as a
character.
 
J

Jay

Thanks Jon, but it's still not clear to me - what you have quoted is what I read in Help, which I
didn't understand.

What is meant by "any numeric Unicode category"? Does that mean decimal digits 0-9 AND some other
characters too? If so, what are the other characters?

"The input method is irrelevant to the value when you've got it as a character" ...but the character
is from a string which comes from a keyboard.



I want to check if a character is a decimal digit (0-9).

I've found Char.IsNumber() and Char.IsDigit() but couldn't work out
from Help how they differ.

From the docs for IsNumber:

<quote>
This method determines if a Char is of any numeric Unicode category.
This contrasts with IsDigit, which determines if a Char is a radix-10
digit.

Valid numbers are members of the following categories in
UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
Also, do I need to worry about how digits get converted when typed
into keyboards in different countries? Can I assume that
Char.IsNumber() and Char.IsDigit() cope with this?

The input method is irrelevant to the value when you've got it as a
character.
 
J

Jon Skeet [C# MVP]

Thanks Jon, but it's still not clear to me - what you have quoted is
what I read in Help, which I didn't understand.

What is meant by "any numeric Unicode category"? Does that mean
decimal digits 0-9 AND some other characters too? If so, what are the
other characters?

Yes, there are other types of number characters, in the categories
LetterNumber and OtherNumber, as specified. www.unicode.org could
probably give you more information.
"The input method is irrelevant to the value when you've got it as a
character" ...but the character is from a string which comes from a
keyboard.

But if the character is '0' it doesn't matter what keys they had to
press in order to get that character.
 
J

Jay

Thanks again for your help Jon.

It sounds like I need to use IsDigit, since I don't want these other characters.

"But if the character is '0' it doesn't matter what keys they had to press in order to get that
character."
I wondered if there are different types of decimal digits, apart from those in Ascii. Unicode
confuses me - are there lots of representations of '0', or just one (the Ascii one?

Jay

Thanks Jon, but it's still not clear to me - what you have quoted is
what I read in Help, which I didn't understand.

What is meant by "any numeric Unicode category"? Does that mean
decimal digits 0-9 AND some other characters too? If so, what are the
other characters?

Yes, there are other types of number characters, in the categories
LetterNumber and OtherNumber, as specified. www.unicode.org could
probably give you more information.
"The input method is irrelevant to the value when you've got it as a
character" ...but the character is from a string which comes from a
keyboard.

But if the character is '0' it doesn't matter what keys they had to
press in order to get that character.
 
J

Jon Skeet [C# MVP]

Thanks again for your help Jon.

It sounds like I need to use IsDigit, since I don't want these other
characters.

Right. I strongly suspect you're right.
"But if the character is '0' it doesn't matter what keys they had to
press in order to get that character." I wondered if there are
different types of decimal digits, apart from those in Ascii. Unicode
confuses me - are there lots of representations of '0', or just one
(the Ascii one?

AFAIK, there's only one decimal digit '0' character.
 
J

Jay

Laura: thanks for the useful link. I don't want the function to pick up on things that IsNumber does
(eg Roman numerals, such as i, v, x) so I'll use IsDigit.

Jay

A quick reference for unicode number,letter category:
http://www.fileformat.info/info/unicode/category/Nl/list.htm

For these, IsNumber returns true whereas IsDigit returns false. For example.
IsNumber returns true when IsDigit returns true, but not vice versa.
The performance seems the same to me, so why not just use IsNumber and leave
IsDigit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top