本文整理匯總了C#中System.Math.Sqrt方法的典型用法代碼示例。如果您正苦於以下問題:C# Math.Sqrt方法的具體用法?C# Math.Sqrt怎麽用?C# Math.Sqrt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Math
的用法示例。
在下文中一共展示了Math.Sqrt方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: foreach
// Create an array containing the area of some squares.
Tuple<string, double>[] areas =
{ Tuple.Create("Sitka, Alaska", 2870.3),
Tuple.Create("New York City", 302.6),
Tuple.Create("Los Angeles", 468.7),
Tuple.Create("Detroit", 138.8),
Tuple.Create("Chicago", 227.1),
Tuple.Create("San Diego", 325.2) };
Console.WriteLine("{0,-18} {1,14:N1} {2,30}\n", "City", "Area (mi.)",
"Equivalent to a square with:");
foreach (var area in areas)
Console.WriteLine("{0,-18} {1,14:N1} {2,14:N2} miles per side",
area.Item1, area.Item2, Math.Round(Math.Sqrt(area.Item2), 2));
輸出:
City Area (mi.) Equivalent to a square with: Sitka, Alaska 2,870.3 53.58 miles per side New York City 302.6 17.40 miles per side Los Angeles 468.7 21.65 miles per side Detroit 138.8 11.78 miles per side Chicago 227.1 15.07 miles per side San Diego 325.2 18.03 miles per side
示例2: Main
//引入命名空間
using System;
class MainClass {
public static void Main() {
double s1 = 3.0;
double s2 = 4.0;
double hypot;
hypot = Math.Sqrt(s1*s1 + s2*s2);
Console.WriteLine("Hypotenuse is " + hypot);
}
}