Csharp/C Sharp/Data Types/double
Содержание
- 1 Compute the area of a circle
- 2 converts Fahrenheit to Celsius
- 3 double number format: 0:C, 0:D9, 0:E, 0:F3, 0:N, 0:X, 0:x
- 4 Epsilon, PositiveInfinity, NegativeInfinity, MaxValue, MinValue
- 5 Format double value
- 6 Implement the Pythagorean Theorem
- 7 Talking to Mars: double value calculation
- 8 the differences between int and double
Compute the area of a circle
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Compute the area of a circle.
using System;
public class ComputeCircle {
static void Main() { double radius; double area; radius = 10.0; area = radius * radius * 3.1416; Console.WriteLine("Area is " + area); }
}
</source>
converts Fahrenheit to Celsius
<source lang="csharp"> /*
This program converts Fahrenheit to Celsius. Call this program FtoC.cs.
- /
using System;
public class FtoC {
public static void Main() { double f; // holds the temperature in Fahrenheit double c; // holds the temparture in Celsius f = 59.0; // start with 59 degrees Fahrenheit c = 5.0 / 9.0 * (f - 32.0); // convert to Celsius Console.Write(f + " degrees Fahrenheit is "); Console.WriteLine(c + " degrees Celsius."); }
}
</source>
double number format: 0:C, 0:D9, 0:E, 0:F3, 0:N, 0:X, 0:x
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { Console.WriteLine("C format: {0:C}", 99989.987); Console.WriteLine("D9 format: {0:D9}", 99999); Console.WriteLine("E format: {0:E}", 99999.76543); Console.WriteLine("F3 format: {0:F3}", 99999.9999); Console.WriteLine("N format: {0:N}", 99999); Console.WriteLine("X format: {0:X}", 99999); Console.WriteLine("x format: {0:x}", 99999); }
}
</source>
Epsilon, PositiveInfinity, NegativeInfinity, MaxValue, MinValue
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { Console.WriteLine("-> double.Epsilon: {0}", double.Epsilon); Console.WriteLine("-> double.PositiveInfinity: {0}", double.PositiveInfinity); Console.WriteLine("-> double.NegativeInfinity: {0}", double.NegativeInfinity); Console.WriteLine("-> double.MaxValue: {0}", double.MaxValue); Console.WriteLine("-> double.MinValue: {0}", double.MinValue); }
}
</source>
Format double value
<source lang="csharp"> using System; public class NumParsingApp {
public static void Main(string[] args) { int i = int.Parse("12345"); Console.WriteLine("i = {0}", i); int j = Int32.Parse("12345"); Console.WriteLine("j = {0}", j); double d = Double.Parse("1.2345E+6"); Console.WriteLine("d = {0:F}", d); string s = i.ToString(); Console.WriteLine("s = {0}", s); }
}
</source>
Implement the Pythagorean Theorem
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Implement the Pythagorean Theorem.
using System;
public class Pythagorean {
public static void Main() { double s1; double s2; double hypot; string str; Console.WriteLine("Enter length of first side: "); str = Console.ReadLine(); s1 = Double.Parse(str); Console.WriteLine("Enter length of second side: "); str = Console.ReadLine(); s2 = Double.Parse(str); hypot = Math.Sqrt(s1*s1 + s2*s2); Console.WriteLine("Hypotenuse is " + hypot); }
}
</source>
Talking to Mars: double value calculation
<source lang="csharp"> /* C# A Beginner"s Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295
- /
/*
Project 2-1 Talking to Mars Call this file mars.cs
- /
using System;
public class Mars {
public static void Main() { double distance; double lightspeed; double delay; double delay_in_min; distance = 34000000; // 34,000,000 miles lightspeed = 186000; // 186,000 per second delay = distance / lightspeed; Console.WriteLine("Time delay when talking to Mars: " + delay + " seconds."); delay_in_min = delay / 60; Console.WriteLine("This is " + delay_in_min + " minutes."); }
}
</source>
the differences between int and double
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
/*
This program illustrates the differences between int and double.
- /
using System;
public class Example3IntDouble {
public static void Main() { int ivar; // this declares an int variable double dvar; // this declares a floating-point variable ivar = 100; // assign ivar the value 100 dvar = 100.0; // assign dvar the value 100.0 Console.WriteLine("Original value of ivar: " + ivar); Console.WriteLine("Original value of dvar: " + dvar); Console.WriteLine(); // print a blank line // now, divide both by 3 ivar = ivar / 3; dvar = dvar / 3.0; Console.WriteLine("ivar after division: " + ivar); Console.WriteLine("dvar after division: " + dvar); }
}
</source>