NEWS

[C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int

[C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
Đăng bởi: Thảo Meo - Lượt xem: 2067 08:46:57, 02/06/2023C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách chuyển đổi kiểu dữ liệu String sang kiểu số nguyên Int trong lập trình C#.

[C#] Các cách chuyển đổi kiểu chữ String sang kiểu số nguyên Int

string_to_int

Chúng ta có ba cách để thực hiện chuyển đổi:

Cách 1: Sử dụng hàm Parse

Cú pháp:

Parse(string s)
Parse(string s, numberstyle style)
Parse(String s, NumberStyles style, IFormatProvider provider)

Ví dụ: 

Int16.Parse("100"); // returns 100
Int16.Parse("(100)", NumberStyles.AllowParentheses); // returns -100

int.Parse("30,000", NumberStyles.AllowThousands, new CultureInfo("en-au"));// returns 30000
int.Parse("$ 10000", NumberStyles.AllowCurrencySymbol); //returns 10000
int.Parse("-100", NumberStyles.AllowLeadingSign); // returns -100
int.Parse(" 100 ", NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite); // returns 100

Int64.Parse("2147483649"); // returns 2147483649

Ở các trên các bạn thấy hàm này cung cấp cho chúng ta các tham số để chúng ta có thể chuyển đổi string sang kiểu số nguyên dễ dàng.

Cách 2: Sử dụng Class Convert

Convert.ToInt16("100"); // returns short
Convert.ToInt16(null);//returns 0

Convert.ToInt32("233300");// returns int
Convert.ToInt32("1234",16); // returns 4660 - Hexadecimal of 1234

Convert.ToInt64("1003232131321321");//returns long

// the following throw exceptions
Convert.ToInt16("");//throws FormatException
Convert.ToInt32("30,000"); //throws FormatException
Convert.ToInt16("(100)");//throws FormatException
Convert.ToInt16("100a"); //throws FormatException
Convert.ToInt16(2147483649);//throws OverflowException

ở ví dụ trên, các bạn thấy khi chúng ta sử dụng hàm này để convert nó sẽ xuất không xuất hiện lỗi

VD: Convert.ToInt16(null), bình thường nếu bạn dùng hàm Parse trên để chuyển đổi nó văng lỗi ngay.

Cách 3: Sử dụng Hàm TryPase

Cú pháp:

bool Int32.TryParse(string s, out int result)
bool Int32.TryParse(string s, NumberStyle style, IFormatProvider provider, out int result)

Khi sử dụng hàm TryParse, nó sẽ trả về cho chúng ta kiểu dữ liệu True/False.

+ Nếu True chuỗi dữ liệu này có thể chuyển đổi sang kiểu số.

+ Nếu False không thể chuyển đổi được, bạn ơi.

Ví dụ:

string numberStr = "123456";
int number;

bool isParsable = Int32.TryParse(numberStr, out number);

if (isParsable)
    Console.WriteLine(number);
else
    Console.WriteLine("Could not be parsed.");

Với ví dụ trên này nó chuyển đổi thành công.

string numberStr = "123456as";
int number;

bool isParsable = Int32.TryParse(numberStr, out number);
if (isParsable)
    Console.WriteLine(number);
else
    Console.WriteLine("Could not be parsed.");

Còn ở ví dụ này, nó sẻ trả về kết quả "Could not be parsed."

 

Thanks for watching!

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
Đăng bởi: Thảo Meo - Lượt xem: 2067 08:46:57, 02/06/2023C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.