Csharp/CSharp Tutorial/Data Type/float
Содержание
Float Literals
<source lang="csharp">using System; class MainClass {
static void Main() { Console.WriteLine("{0}", 3.1416F); // float literal }
}</source>
3.1416
floats and arithmetic operators
<source lang="csharp">class MainClass {
public static void Main() { System.Console.WriteLine("10f / 3f = " + 10f / 3f); float floatValue1 = 10f; float floatValue2 = 3f; System.Console.WriteLine("floatValue1 / floatValue2 = " + floatValue1 / floatValue2); }
}</source>
10f / 3f = 3.333333 floatValue1 / floatValue2 = 3.333333
Pass float to a function
<source lang="csharp">using System; class MainClass {
public static void Main() { float f = 6.0F; float s = Avg(5.0F, f); Console.WriteLine(s); } public static float Avg(float Input1, float Input2) { return (Input1 + Input2) / 2.0F; }
}</source>
5.5
Use Remainder Operator on float point data type
<source lang="csharp">using System; class MainClass {
static void Main() { Console.WriteLine("0.0f % 1.3f is {0}", 0.0f % 1.3f); Console.WriteLine("0.3f % 1.3f is {0}", 0.3f % 1.3f); Console.WriteLine("1.0f % 1.3f is {0}", 1.0f % 1.3f); Console.WriteLine("1.3f % 1.3f is {0}", 1.3f % 1.3f); Console.WriteLine("2.0f % 1.3f is {0}", 2.0f % 1.3f); Console.WriteLine("2.3f % 1.3f is {0}", 2.3f % 1.3f); }
}</source>
0.0f % 1.3f is 0 0.3f % 1.3f is 0.3 1.0f % 1.3f is 1 1.3f % 1.3f is 0 2.0f % 1.3f is 0.7 2.3f % 1.3f is 1