2020年11月21日 星期六

[ C# ] 資料轉換之數值與字串

一、數值轉換字串

官網:https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number

1. 整數轉字串、字串轉數值:

整數轉字串:Convert.ToString(整數)

字串轉數值:Convert.ToInt32("內容");

2. 字串轉數值:Parse(字串), 方法如下:

整數:int.Parse(字串)

倍精確度:double.Parse(字串)

浮點數:float.Parse(字串)

二、明確轉換

官網:https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/types/casting-and-type-conversions

這方法不能用到字串(String)。

1. 數值

倍精確度:double y = 2.55;

浮點數:float j = 134.45E-2f;

2. 例:轉換成整數 

int x = (int)j;

三、自動轉換

以下能自動轉換的型別。

1. sbyte:short、int、long、float、double、decimal。

2. byte:short、ushort、int、uint、long、ulong、float、double、decimal

3. short:int、long、float、double、decimal

4. ushort:int、uint、long、ulong、float、double、decimal

5. int:long、float、double、decimal

6. uint:long、ulong、float、double、decimal

7. long:float、double、decimal

8. ulong:float、double、decimal

9. float:double

10. char:ushort、int、uint、long、ulong、float、double、decimal


沒有留言:

張貼留言

影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。