convert string to Double to get the subtotal

1

I'm new to c # and I have the following code:

 using System;

public class Program
{
    public static void Main()

    {

        int precio;
        int cantidadarticulo;
        int subtotal;
        int siniva;
        int coniva;
        int total;

     Console.WriteLine("dame nombre : ");
     string nombrepedido = Console.ReadLine();

     Console.WriteLine("dame precio : ");
     string preciopedidos = Console.ReadLine();
     precio = Convert.ToInt32(preciopedidos);


     Console.WriteLine("dame cantidad de cualquier articulo : ");
     string cantidadarticulopedido = Console.ReadLine();
     cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);


        //calcular subtotal
        subtotal = cantidadarticulo/16;

and if you see in this next line I'm using toDouble to convert a String to double to get the subtotal.

 cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);

For that I'm using the following c # code:

Convert.ToDouble Method (String)

origin of the code above

this is the complete code:

 //+===================================================================
//
//   Acerca del programa : 
//
//   3. Elabore un programa que pida el nombre, el precio y la cantidad de cualquier artículo (sin iva); 
//   y que después informe de cuanto tiene que pagar el usuario por dicho producto: subtotal sin iva, iva y total.  
//
//  Autor del codigo : Gilberto Quintero Armenta
//  codigo en github ( el codigo en linea ) :

//  Herramienta que use para codificar el codigo c# : https://dotnetfiddle.net
//
//
//
//
//
//+===================================================================

using System;

public class Program
{
    public static void Main()

    {

        int precio;
        int cantidadarticulo;
        int subtotal;
        int siniva;
        int coniva;
        int total;

     Console.WriteLine("dame nombre : ");
     string nombrepedido = Console.ReadLine();

     Console.WriteLine("dame precio : ");
     string preciopedidos = Console.ReadLine();
     precio = Convert.ToInt32(preciopedidos);


     Console.WriteLine("dame cantidad de cualquier articulo : ");
     string cantidadarticulopedido = Console.ReadLine();
     cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);


        //calcular subtotal
        subtotal = cantidadarticulo/16;




        Console.WriteLine("subtototal: ");
        Console.WriteLine("sin iva: ");
        Console.WriteLine("total: ");



    }
}

do not understand why it does not work if the documentation states that I must use toDouble to convert a string to Double, ¿Dondre could be my mistake?

    
asked by Gilberto 17.01.2018 в 19:50
source

2 answers

3

Well, it's simple, I'll explain:

You are declaring an integer

    int cantidadarticulo;

You are trying to assign a double to an integer

     cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);

To solve it declares cantidadarticulo like double;

asi:

double cantidadarticulo;

Note: I suggest declaring your int as decimal, since for example a price can have decimals. As well as the subtotal

    
answered by 17.01.2018 / 20:28
source
0

Although your doubt has already been clarified, I would like to add some suggestions that allow you to write code in C # that is more readable.

To begin with, in C # it is not common to declare the variables at the beginning of Main() , as is usually done in C or C ++, but it is a common practice to declare the variables when using them, as for example you did in the line: string nombrepedido = Console.ReadLine(); Similarly, instead of declaring at the beginning of Main() the variable int cantidadarticulo and then writing:

Console.WriteLine("dame cantidad de cualquier articulo : ");
string cantidadarticulopedido = Console.ReadLine();
cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);

the following code is more readable:

Console.WriteLine("dame cantidad de cualquier articulo : ");
string cantidadarticulopedido = Console.ReadLine();
int cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);

Writing in the previous way would have made you notice your mistake of wanting to store a double in a int , which is not allowed. If you use the VS you will even notice that the program "complains" and points out the error: " Unable to convert the type double in int ".

On the other hand, it is unnecessary to create extra variables of type string to use the Console.ReadLine() method. Instead of writing:

Console.WriteLine("dame precio : ");
string preciopedidos = Console.ReadLine();
precio = Convert.ToInt32(preciopedidos);

I suggest you write more succinctly the previous code as:

Console.WriteLine("dame precio : ");
int precio = Convert.ToInt32(Console.ReadLine());

The above is also a common practice in C #.

To finish, with respect to the name of your variables. As I understand, when the name of your variable is composed of two or more words, as is the case of cantidadarticulo , it is not recommended to write everything in lowercase, but rather make use of the sub-script (_) to separate each word or Capitalize the first letter of each word from the second. For example, in the case of the variable cantidadarticulo , it is better to write either cantidad_articulo or cantidadArticulo .

    
answered by 17.01.2018 в 21:55