Diferentes formas de tomar entrada e imprimir un valor flotante en C#

En C# , sabemos que el método Console.ReadLine() se usa para leer strings desde el dispositivo de salida estándar. Luego, este valor se convierte. Hay diferentes métodos disponibles para convertir la entrada tomada en un valor flotante.

  • Método Single.Parse()
  • Método float.Parse()
  • Método Convert.ToSingle()

Método Single.Parse()

El método Single.Parse()

  • Único
  • Analizar gramaticalmente()

Sintaxis:

float_value = Single.Parse(Console.ReadLine());

Ejemplo: tomar la entrada de un valor flotante usando el método Single.Parse()

C#

// C# program to Take input 
// of a float value using 
// Single.Parse() Method
    
using System;
using System.Text;
  
public class GFG{
      
    // Main Method
    static void Main(string[] args)
    {
        //declaring a float variables
        float value = 0.0f;
          
        // use of Single.Parse() Method
        value = Single.Parse(Console.ReadLine());
      
        //printing the value
        Console.WriteLine("Value = {0}", value);
    }
}

Entrada de la consola:

12.34

Producción:

Value = 12.34

Método float.Parse()

El método float.Parse()

  • flotar solo
  • Analizar gramaticalmente()

Sintaxis:

float_value = float.Parse(Console.ReadLine());

Ejemplo: tomar la entrada de un valor flotante usando el método float.Parse()

C#

// C# program to Take input 
// of a float value using 
// float.Parse() Method
    
using System;
using System.Text;
  
public class GFG{
      
    // Main Method
    static void Main(string[] args)
    {
        // declaring a float variables
        float value = 0.0f;
          
        // use of float.Parse() Method
        value = float.Parse(Console.ReadLine());
      
        // printing the value
        Console.WriteLine("Value = {0}", value);
    }
}

Entrada de la consola:

12.34

Producción:

Value = 12.34

Método Convert.ToSingle()

El método Convert.ToSingle()

  • Convertir
  • ParaSingle()

Sintaxis:

float_value = Convert.ToSingle(Console.ReadLine());

Ejemplo: tomar la entrada de un valor flotante usando el método Convert.ToSingle()

C#

// C# program to Take input 
// of a float value using 
// Convert.ToSingle() method
    
using System;
using System.Text;
  
public class GFG{
      
    // Main Method
    static void Main(string[] args)
    {
        // declaring a float variable
        float value = 0.0f;
          
        // use of Convert.ToSingle() Method
        value = Convert.ToSingle(Console.ReadLine());
      
        // printing the value
        Console.WriteLine("Value = {0}", value);
    }
}

Entrada de la consola:

12.34

Producción:

Value = 12.34

Publicación traducida automáticamente

Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *