["c# double","","37 C# ","A3.yry.wrrytsefyzXBaCECECST~~}T~~YFG74666564F53CaBCEP64G4G4G4G4G4GA964G4G84A96FsBCXCC#BCE.S}T~~}T~~","Double"," is an 8-byte numeric type. It is used to store large and small values. It also stores fractional values such as 1.5 and negative values such as -1.5. It requires more memory than an int. ","Single, Double ","single-double","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. It accommodates large and small numbers. ","Encoding: ","Its encoding uses 8 bytes, twice the number of bytes in an int. The additional 4 bytes allow more representations in the type.","Int ","int","Next: ","We show that the double is aliased (mapped) to the System.Double struct\u2014the two types are equivalent.","Struct ","struct","MaxValue: ","The maximum value of a double is expressed in scientific notation\u2014after E we have the power of 308.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.7 (2017)\n\n","C# program that uses double type","\n\nusing System;\n\nclass Program\n{\n static void Main()\n {","\n // Use double type.\n ","double"," number = ","1.5",";\n Console.WriteLine(number);\n number = -1;"," // Can be negative\n ","Console.WriteLine(number);\n Console.WriteLine(number == -1);"," // Can use == operator\n ","Console.WriteLine(number + 100);"," // Can use + operator\n ","Console.WriteLine(number.GetType());\n Console.WriteLine(typeof(double));\n Console.WriteLine(double.MinValue);\n Console.WriteLine(","double.MaxValue",");","\n\n // Find the memory usage for a double value.\n ","long bytes1 = GC.GetTotalMemory(false);\n double[] array = new double[1000 * 1000];\n array[0] = 1;\n long bytes2 = GC.GetTotalMemory(false);\n Console.WriteLine(","\"{0} bytes per double\"",", ((bytes2 - bytes1) / (1000 * 1000)));\n }\n}\n\n","Output","\n\n1.5\n-1\nTrue\n99\nSystem.Double\nSystem.Double\n-1.79769313486232E+308\n","1.79769313486232E+308","\n8 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. This typically requires the bits to be copied into another memory location. ","And: ","If you use an integer, only four bytes will be copied. If you use a double, eight bytes. The extra copying impacts performance.","Parse, TryParse."," The double.Parse and double.TryParse methods are static\u2014you call them on the \"double\" type. The difference between the two methods is that double.Parse throws exceptions, while double.TryParse does not. ","Static Method ","static","Next: ","We see an example that demonstrates the different strings double.Parse methods handle.","Strings ","string","C# program that uses double.Parse","\n\nusing System;\n\nclass Program\n{\n static void Main()\n {","\n //\n // Usage of double.Parse on various input strings.\n //\n ","string[] tests = new string[]\n {\n ","\"1,000.00\"",","," // <-- This is 1000\n ","\"1.000\"",","," // <-- This is 1\n ","\"0.201\"",","," //\n ","\"00.001\"",","," // <-- This is 0.001\n ","\"-0.01\"",","," // <-- This is -0.01\n ","\"500000000\"",","," // <-- Five-hundred million\n ","\"0.0\""," // <-- 0\n ","};\n\n foreach (string test in tests)\n {\n double value = ","double.Parse","(test);\n Console.WriteLine(value);\n }","\n\n //\n // Usage of double.TryParse on various unusual inputs\n //\n ","string[] unusuals = new string[]\n {\n ","\"NaN\"",","," // <-- This can be parsed.\n ","\"MaxValue\"",","," // <-- This fails.\n ","\"NegativeInfinity\"",",\n ","\"Programmer\"",",\n ","\"0.01-0.02\"",",\n ","\" 0\""," // <-- This succeeds and is 0.\n ","};\n\n foreach (string unusual in unusuals)\n {\n double value;\n if (","double.TryParse","(unusual, out value))"," // Returns bool\n ","{\n Console.WriteLine(\"Valid: {0}\", value);\n }\n }\n }\n}\n\n","Output","\n\n1000\n1\n0.201\n0.001\n-0.01\n500000000\n0\nValid: NaN\nValid: 0","The double.TryParse method"," is an instance of the tester-doer pattern. This pattern describes methods that see if some action can be done before actually doing it. This removes the possibility of a parsing error. ","Tester-Doer ","tester-doer","Tip: ","Using double.TryParse will enhance performance if you deal with lots of invalid input.","Correctly parsed strings."," You can have numbers with commas for thousands, excess decimal places with zeros, excess leading zeros, negative signs, large numbers such as 500 million, 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.","These two parsing methods"," are invaluable for when you are storing percentages in text files or databases. And for large numbers that are not monetary values, double.Parse is useful. ","Percentage ","percentage","However: ","When dealing with currency values, we should investigate the decimal type and its parsing methods.","Decimal ","decimal","Summary."," As a low-level type, the double uses eight bytes of memory. As a value type, it has clear efficiency advantages over any possible object-based replacements. For accurate representations, you can instead choose the decimal type. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"]