More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Thursday, 28 February 2019 7:22:23 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 501

c# convert char to int

How to convert the character that represents a number to a numeric value type?

1.
//since GetNumericValue returns double, cast to int
var intValue= (int)char.GetNumericValue(myChar);

2.
var intValue= int.Parse(myChar.ToString());

better to play safe,
if (!int.TryParse(myChar.ToString(), out intValue))
{
//handle the error
}

3.
var intValue = myChar - '0';
or
//Each character is internally represented by a number and the characters '0' to '9' are represented by consecutive numbers.
//the decimal value of character 0 in ASCII code is 48.
var intValue = myChar - 48;

Note that Convert.ToByte(myChar) returns the value of the character, not the represented a number.






Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.