打開檢視 > 其他視窗 > C#互動,可以不用編譯直接顯示程式碼結果。
1. 數字類型型別極限值
> int.MaxValue // 2147483647
> int.MinValue // -2147483648
> double.MaxValue // 1.7976931348623157E+308
> double.MinValue // -1.7976931348623157E+308
> decimal.MaxValue // 79228162514264337593543950335
> decimal.MinValue // -79228162514264337593543950335
2. stirng.Trim、string.Substring、string.Length 方法
> string text = " abcde ";
> text.Length // 9。前後各有兩格空格。
> text.Trim() // "abcde"。刪掉前後空格。另有TrimStart、TrimEnd。
> text.Trim().Length // 5
> text.Substring(4) // "cde "。01234,所以對應到字串是「空格、空格、a、b、c...」的 'c','c' 以前的都字元都刪掉。
> text.Substring(3, 4) // "bcde"。從index 3開始的4個字元。可以記成start, range(int startIndex, int length)
3. float的小數點要補'f'
> float pi = 3.14;
(1,12): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
> decimal money = 10.99;
(1,17): error CS0664: Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type
> float pi = 3.14f; // pass.
> decimal money = 10.99m; // pass.
4. string.Format 方法
> var city = "Dallas";
> var temperature = 103.4f;
> var currentDt = DateTime.Now();
(1,26): error CS1955: Non-invocable member 'DateTime.Now' cannot be used like a method.
> var currentDt = DateTime.Now;
> string.Format($"Welcome to {city}. The time is {currentDt}. The temperature is {temperature}.")
"Welcome to Dallas. The time is 2020/12/4 下午 04:36:31. The temperature is 103.4."
> string.Format($"Welcome to {city}. The time is {currentDt:t}. The temperature is {temperature}.")
"Welcome to Dallas. The time is 下午 04:36. The temperature is 103.4."
> string.Format($"Welcome to {city}. The time is {currentDt:T}. The temperature is {temperature}.")
"Welcome to Dallas. The time is 下午 04:36:31. The temperature is 103.4."
> temperature = 1000.25f
100.25
> temperature = 1000.25d
(1,15): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
> temperature = 1000.25m
(1,15): error CS0266: Cannot implicitly convert type 'decimal' to 'float'. An explicit conversion exists (are you missing a cast?)
> string.Format($"Welcome to {city}. The time is {currentDt:T}. The temperature is {temperature:0,0.00}.")
"Welcome to Dallas. The time is 下午 04:36:31. The temperature is 1,000.25."
5. int.TryParse、string.Replace 方法
> int.Parse("15.2334") // 調皮一下。
System.FormatException: 輸入字串格式不正確。+ System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool) + ... + <Initialize>.MoveNext()
> string test = "15,234";
> test.Replace(',', '') // 雷!不可以空字元!
(1,19): error CS1011: Empty character literal
> test.Replace(",", "") // 空字串才可以!!
"15234"
> int result;
> int.TryParse("15,234", out result) // true就指派給result
false
> int.TryParse("15234", out result)
true
> result
15234 // 死勾蟻爹死餒!
6. Math 類別
(需要背一下的靜態方法...)
Sqrt // 平方根
Pow(value, power) // 幾次方。印象中在C++ Math Library是value^power。
> Math.Pow(4, 0.5) // 2。平方根也可以寫成二分之一次方。
> Math.Pow(8, 1/3d) // 2。開三方,記得將power值切成double型別。
Round、Ceiling、Floor // 四捨五入、無條件進位、捨去至整數
Abs // 絕對值
Log(Double, Double) // 傳回指定底數中指定數字的對數。
Log(Double) // 傳回指定數字的自然對數 (底數為 e) 。
Log10(Double) // 傳回指定數字的以 10 為底數的對數。
Log2(Double) // 傳回指定數字的以 2 為底數的對數。
(未完待續...)
沒有留言:
張貼留言