____  ___    _  _     _   _ _____     _______
 / ___|/ _ \  | || |   | | | |_ _\ \   / / ____|
| |  _| | | | | || |_  | |_| || | \ \ / /|  _|
| |_| | |_| | |__   _| |  _  || |  \ V / | |___
 \____|\___/     |_|   |_| |_|___|  \_/  |_____|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

[C#] 문자를 숫자형식으로 변환

BY: @devhongjinhyeon | CREATED: Jan. 12, 2018, 6 a.m. | VOTES: 0 | PAYOUT: $0.00 | [ VOTE ]

문자형 변수를 숫자형으로 변환에는 2가지의 방법이 있습니다.

  1. 해당 타입의 Parse() 메소드 사용
  2. Convert 클래스 사용

우선 예제를 보겠습니다.


string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";

int result;
bool success;

result = Int32.Parse(s1); //-- 1234 
result = Int32.Parse(s2); //-- FormatException 
result = Int32.Parse(s3); //-- ArgumentNullException 
result = Int32.Parse(s4); //-- OverflowException 

result = Convert.ToInt32(s1); //-- 1234 
result = Convert.ToInt32(s2); //-- FormatException 
result = Convert.ToInt32(s3); //-- 0 
result = Convert.ToInt32(s4); //-- OverflowException 

success = Int32.TryParse(s1, out result); //-- success => true; result => 1234 
success = Int32.TryParse(s2, out result); //-- success => false; result => 0 
success = Int32.TryParse(s3, out result); //-- success => false; result => 0 
success = Int32.TryParse(s4, out result); //-- success => false; result => 0 

TAGS: [ #csharp ] [ #convert ] [ #string ] [ #int ]

Replies

@steemitboard | Jan. 11, 2019, 2:19 a.m. | Votes: 0 | [ VOTE ]

Congratulations @devhongjinhyeon! You received a personal award!

https://steemitimages.com/70x70/http://steemitboard.com/@devhongjinhyeon/birthday1.png1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard RankingSteemitBoard - Witness Update

> Support SteemitBoard's project! Vote for its witness and get one more award!

@steemitboard | Jan. 11, 2020, 2:50 a.m. | Votes: 0 | [ VOTE ]

Congratulations @devhongjinhyeon! You received a personal award!

https://steemitimages.com/70x70/http://steemitboard.com/@devhongjinhyeon/birthday2.pngHappy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!
[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>