예제 #1
0
        private static void Info_Double()
        {
            // Formatting floating point numbers (of type "double")
            // Note: Similar to float, so this section is simpler
            Console.WriteLine("### Doubles ### (similar to floats)");
            double d = 123.0987654321;

            Console.WriteLine("  A = {0} [raw]", d);
            Console.WriteLine();

            Console.WriteLine("(using a method to display a variable number of decimal places)");
            for (int places = 0; places <= 10; places++)
            {
                Console.WriteLine("  decimals = {0,2}, output = {1}", places, StringLibrary.FormatFloat(d, places));
            }

            Console.WriteLine();

            Console.WriteLine("(converting between a double and string)");
            double dOrig = 3.14159265358979323846;
            string s1    = dOrig.ToString();
            string s2    = dOrig.ToString("R");   // "Round-trip" specifier (attempts to ensure string is parsed back to the original)
            string s3    = dOrig.ToString("G17"); // Recommended specifier due to bugs and performance of the "R" specifier
            double d1    = Convert.ToDouble(s1);
            double d2    = Convert.ToDouble(s2);
            double d3    = Convert.ToDouble(s3);

            Console.WriteLine("  Original = {0:0.#################}\t\t\t\t[original]", dOrig);
            Console.WriteLine("  s1 = {0,-20}d1 = {1:0.#################}\t[ToString()]", s1, d1);
            Console.WriteLine("  s2 = {0,-20}d2 = {1:0.#################}\t[ToString(\"R\")]", s2, d2);
            Console.WriteLine("  s3 = {0,-20}d3 = {1:0.#################}\t[ToString(\"G17\")]", s3, d3);
            Console.WriteLine("#\n");
        }
예제 #2
0
        private static void Info_Float()
        {
            // Formatting floating point numbers (of type "float")
            Console.WriteLine("### Floats ###");
            float f1 = 0.0f;
            float f2 = -3.14159265359f;
            float f3 = 123456.789f;

            Console.WriteLine("  A = {0}, B = {1}, C = {2}\t\t[raw]", f1, f2, f3);
            Console.WriteLine("  A = {0:0.###}, B = {1:0.###}, C = {2:0.###}\t\t[max decimal places using \"0:0.#...\"]", f1, f2, f3);
            Console.WriteLine("  A = {0:0.000}, B = {1:0.000}, C = {2:0.000}\t\t[fixed decimal places using \"0:0.0...\"]", f1, f2, f3);
            Console.WriteLine("  A = {0:F3}, B = {1:F3}, C = {2:F3}\t\t[fixed decimal places using \"0:F*\"]", f1, f2, f3);
            Console.WriteLine("  A = {0:N3}, B = {1:N3}, C = {2:N3}\t[thousand separator and fixed dps using \"0:N*\"]", f1, f2, f3);
            Console.WriteLine();

            Console.WriteLine("(formatting floats with padding)");
            f1 = 3.141593f;
            Console.WriteLine("  A = |{0}|\t\t[raw]", f1);
            Console.WriteLine("  A = |{0,12}|\t\t[left padding, raw using \"0,N\"]", f1);
            Console.WriteLine("  A = |{0,-12}|\t\t[right padding, raw using \"0,-N\"]", f1);
            Console.WriteLine("  A = |{0,12:0.00}|\t\t[left padding, fixed decimal places using \"0,N:0.0...\"]", f1);
            Console.WriteLine("  A = |{0,-12:0.00}|\t\t[right padding, fixed decimal places using \"0,-N:0.0...\"]", f1);
            Console.WriteLine("  A = |{0,12:F2}|\t\t[left padding, fixed decimal places using \"0,N:F*\"]", f1);
            Console.WriteLine("  A = |{0,-12:F2}|\t\t[right padding, fixed decimal places using \"0,-N:F*\"]", f1);
            Console.WriteLine();

            Console.WriteLine("(using a method to display a variable number of decimal places)");
            for (int places = 0; places <= 5; places++)
            {
                Console.WriteLine("  decimals = {0}, output = {1}", places, StringLibrary.FormatFloat(f2, places));
            }
            Console.WriteLine();

            Console.WriteLine("(demonstrate method can handle template floating point types)");
            double  typeDouble  = 3.141593;
            decimal typeDecimal = 3.141593m;

            Console.WriteLine("  [Double]  {0}", StringLibrary.FormatFloat(typeDouble, 5));
            Console.WriteLine("  [Decimal] {0}", StringLibrary.FormatFloat(typeDecimal, 5));
            Console.WriteLine();

            Console.WriteLine("(using different formatting for positive and negative numbers, and zero)");
            string formatter = "{0:0.000;(0.000);zero}";    // The string "zero" can be anything

            Console.WriteLine("  A = {0}, B = {1}, C = {2}",
                              string.Format(formatter, f1),
                              string.Format(formatter, f2),
                              string.Format(formatter, f3));
            Console.WriteLine();

            Console.WriteLine("(pre-defined formats for floating point numbers)");
            Console.WriteLine("(generally two decimal places; add a number after the format symbol for different dps)");
            f1 = 0.1532f;
            Console.WriteLine("  A = {0:0.0000}\t\t\t[original]", f1);
            Console.WriteLine("  A = {0:C}\t\t\t[using 0:C]", f1);
            Console.WriteLine("  A = {0:E}\t\t[using 0:E]", f1);
            Console.WriteLine("  A = {0:F}\t\t\t[using 0:F (default of 2 decimal places)]", f1);
            Console.WriteLine("  A = {0:G}\t\t\t[using 0:G]", f1);
            Console.WriteLine("  A = {0:N}\t\t\t[using 0:N]", f1);
            Console.WriteLine("  A = {0:P}\t\t\t[using 0:P]", f1);
            Console.WriteLine();

            Console.WriteLine("(list of basic methods to format a floating point number)");
            Console.WriteLine("  A = {0:0.0000}\t\t\t[using \"{{0:0.0...}}\"]", f1);
            Console.WriteLine("  A = {0}\t\t\t[custom function using \"{{0:F*}}\" internally]", StringLibrary.FormatFloat(f1, 4));
            Console.WriteLine("  A = {0}\t\t\t[using System.Single::ToString(format)]", f1.ToString("0.0000"));
            Console.WriteLine();

            Console.WriteLine("(converting between a float and string)");
            float  fOrig = 3.14159265359f;
            string s1    = fOrig.ToString();
            string s2    = fOrig.ToString("R");  // "Round-trip" specifier (attempts to ensure string is parsed back to the original)
            string s3    = fOrig.ToString("G9"); // Recommended specifier due to bugs and performance of the "R" specifier

            f1 = Convert.ToSingle(s1);
            f2 = Convert.ToSingle(s2);
            f3 = Convert.ToSingle(s3);
            Console.WriteLine("  Original = {0:0.#########}\t\t\t[original]", fOrig);
            Console.WriteLine("  s1 = {0,-12}f1 = {1:0.#########}\t[ToString()]", s1, f1);
            Console.WriteLine("  s2 = {0,-12}f2 = {1:0.#########}\t[ToString(\"R\")]", s2, f2);
            Console.WriteLine("  s3 = {0,-12}f3 = {1:0.#########}\t[ToString(\"G9\")]", s3, f3);
            Console.WriteLine("#\n");
        }