C# Double Type: double.MaxValue, double.Parse

Use the double type to store large numbers with fractional values. Double is an 8-byte number.
Double. This is an 8-byte numeric type. Double is used to store large and small values. It also stores fractional values such as 1.5 and negative values such as -1.5.Single, Double
Notes, memory. Double requires more memory than an int. So when specifying the types of fields, it is best to use int when the number does not require the features of a double.Int, uint
An example. A double is declared in the same way as an int. You use the double type in the declaration, and can assign it using the assignment operator "=". It offers fractional values.

Encoding: Its encoding uses 8 bytes, twice the number of bytes in an int. The additional 4 bytes allow more representations in the type.

Next: We show that the double is aliased (mapped) to the System.Double struct—the two types are equivalent.


MaxValue: The maximum value of a double is expressed in scientific notation—after E we have the power of 308.

C# program that uses double type using System; class Program { static void Main() { // Use double type. double number = 1.5; Console.WriteLine(number); number = -1; // Can be negative Console.WriteLine(number); Console.WriteLine(number == -1); // Can use == operator Console.WriteLine(number + 100); // Can use + operator Console.WriteLine(number.GetType()); Console.WriteLine(typeof(double)); Console.WriteLine(double.MinValue); Console.WriteLine(double.MaxValue); // Find the memory usage for a double value. long bytes1 = GC.GetTotalMemory(false); double[] array = new double[1000 * 1000]; array[0] = 1; long bytes2 = GC.GetTotalMemory(false); Console.WriteLine("{0} bytes per double", ((bytes2 - bytes1) / (1000 * 1000))); } } Output 1.5 -1 True 99 System.Double System.Double -1.79769313486232E+308 1.79769313486232E+308 8 bytes per double
Parameters. For developers concerned with performance, double has some drawbacks. When you pass a double as an argument, it is received as a formal parameter.

And: This typically requires the bits to be copied into another memory location.

However: If you use an int, only 4 bytes will be copied. If you use a double, 8 bytes are copied. The extra copying impacts performance.

Test: Here we pass a double to the Test() method. Each invocation of Test (when inlining does not occur) causes the excess memory copying.

C# program that shows double argument using System; class Program { static void Main() { Test(10); } static void Test(double value) { // When called, 8 bytes are copied for the double. Console.WriteLine("8 bytes copied: {0}", value); } } Output 8 bytes copied: 10
Parse, TryParse. The double.Parse and double.TryParse methods are static—you call them on the "double" type. The double.Parse method throws exceptions, while double.TryParse does not.Static

Next: We see an example that demonstrates the different strings double.Parse methods handle.


Info: TryParse() uses the tester-doer pattern. Tester-doer describes methods that see if some action can be done before doing it.

Tip: This removes the possibility of a parsing error. Using double.TryParse will enhance performance if you have invalid input.

C# program that uses double.Parse using System; class Program { static void Main() { // // Usage of double.Parse on various input strings. // string[] tests = new string[] { "1,000.00", // <-- This is 1000 "1.000", // <-- This is 1 "0.201", // "00.001", // <-- This is 0.001 "-0.01", // <-- This is -0.01 "500000000", // <-- Five-hundred million "0.0" // <-- 0 }; foreach (string test in tests) { double value = double.Parse(test); Console.WriteLine(value); } // // Usage of double.TryParse on various unusual inputs // string[] unusuals = new string[] { "NaN", // <-- This can be parsed. "MaxValue", // <-- This fails. "NegativeInfinity", "Programmer", "0.01-0.02", " 0" // <-- This succeeds and is 0. }; foreach (string unusual in unusuals) { double value; if (double.TryParse(unusual, out value)) // Returns bool { Console.WriteLine("Valid: {0}", value); } } } } Output 1000 1 0.201 0.001 -0.01 500000000 0 Valid: NaN Valid: 0
Notes, parsing. Parse() supports commas, excess decimal places with zeros, excess leading zeros, negative signs, large numbers, zero with a decimal, and spaces surrounding the digits.

Note: The Convert.ToDouble method, when called with the string parameter overload, simply calls double.Parse internally after a null check.


Therefore: This method is not useful. But Convert.ToDouble also has other overloads that can be useful when not dealing with strings.

Review, parsing. These 2 parsing methods help when you are storing percentages in text files or databases. And for large numbers that are not monetary values, double.Parse is useful.Percentage

However: When dealing with currency values, we should investigate the decimal type and its parsing methods.

A summary. As a low-level type, double uses 8 bytes of memory. As a value type, it has clear efficiency advantages over any possible object-based replacements.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to