Este método se usa para convertir la representación de string especificada de un número en un número de punto flotante de precisión doble equivalente, usando la información de formato específica de la referencia cultural especificada.
Sintaxis:
doble estático público ToDouble (valor de string, proveedor IFormatProvider);
Parámetros:
- valor: Es una string que contiene el número a convertir.
- proveedor: es un objeto que proporciona información de formato específica de la cultura.
Valor devuelto: este método devuelve un número de punto flotante de doble precisión que es equivalente al número en value , o 0 (cero) si value es nulo .
Excepciones:
- FormatException: si el valor no es un número en un formato válido.
- OverflowException: si el valor representa un número menor que MinValue o mayor que MaxValue .
Los siguientes programas ilustran el uso del método Convert.ToDouble(String, IFormatProvider):
Ejemplo 1:
// C# program to demonstrate the // Convert.ToDouble() Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { try { // Create a NumberFormatInfo object // and set some of its properties. NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = ", "; provider.NumberGroupSeparator = "."; provider.NumberGroupSizes = new int[] { 3 }; // declaring and initializing String array string[] values = {"123456789", "12345.6789", "12345, 6789"}; // calling get() Method Console.Write("Converted decimal value " + "of specified strings: "); for (int j = 0; j < values.Length; j++) { get(values[j], provider); } } catch (FormatException e) { Console.WriteLine("\n"); Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (OverflowException e) { Console.WriteLine("\n"); Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Defining get() method public static void get(string s, NumberFormatInfo provider) { // converting string to specified char double val = Convert.ToDouble(s, provider); // display the converted char value Console.Write(" {0}, ", val); } }
Converted decimal value of specified strings: 123456789, 123456789, 12345.6789,
Ejemplo 2: para FormatException
// C# program to demonstrate the // Convert.ToDouble() Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { try { // Create a NumberFormatInfo object // and set some of its properties. NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = ", "; provider.NumberGroupSeparator = "."; provider.NumberGroupSizes = new int[] { 3 }; // declaring and initializing String array string[] values = {"123456789", "12345.6789", "12345, 6789"}; // calling get() Method Console.Write("Converted double value" + " of specified strings: "); for (int j = 0; j < values.Length; j++) { get(values[j], provider); } Console.WriteLine("\n"); string s = "123 456, 789"; Console.WriteLine("format of s is invalid "); // converting string to specified char double val = Convert.ToDouble(s, provider); // display the converted char value Console.Write(" {0}, ", val); } catch (FormatException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (OverflowException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Defining get() method public static void get(string s, NumberFormatInfo provider) { // converting string to specified char double val = Convert.ToDouble(s, provider); // display the converted char value Console.Write(" {0}, ", val); } }
Converted double value of specified strings: 123456789, 123456789, 12345.6789, format of s is invalid Exception Thrown: System.FormatException
Ejemplo 3: para OverflowException
// C# program to demonstrate the // Convert.ToDouble() Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { try { // Create a NumberFormatInfo object // and set some of its properties. NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = ", "; provider.NumberGroupSeparator = "."; provider.NumberGroupSizes = new int[] { 3 }; // declaring and initializing String array string[] values = { "123456789", "12345.6789", "12345, 6789" }; // calling get() Method Console.Write("Converted decimal value " + "of specified strings: "); for (int j = 0; j < values.Length; j++) { get(values[j], provider); } Console.WriteLine("\n"); string s = "-7922816251426433759354395033500000"; Console.WriteLine("s is less than the MaxValue"); // converting string to specified char decimal val = Convert.ToDecimal(s, provider); // display the converted char value Console.Write(" {0}, ", val); } catch (FormatException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (OverflowException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Defining get() method public static void get(string s, NumberFormatInfo provider) { // converting string to specified char double val = Convert.ToDouble(s, provider); // display the converted char value Console.Write(" {0}, ", val); } }
Converted decimal value of specified strings: 123456789, 123456789, 12345.6789, s is less than the MaxValue Exception Thrown: System.OverflowException
Referencia:
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA