Display product output of two variables with dots instead of comma (c#)

Asked

Viewed 70 times

0

I’m solving an exercise in which it is necessary to declare an employee’s number, how many hours he works and how much he earns per hour, then the system must display this employee’s number and salary, the problem is that the result should be displayed with dots instead of comma in the decimal place, my code so far is this:

using System;
using System.Globalization;

class URI
{

    static void Main(string[] args)
    {
        int numeroFunc, horasTrabalhadas;
        decimal valorHora;

        numeroFunc = int.Parse(Console.ReadLine());
        horasTrabalhadas = int.Parse(Console.ReadLine());
        valorHora = decimal.Parse(Console.ReadLine(), CultureInfo.InvariantCulture);

        Console.WriteLine("NUMBER = " + numeroFunc);
        Console.WriteLine("SALARY = U$ " + (horasTrabalhadas * valorHora));
        Console.ReadLine();
    }

I know I could solve the problem by creating a salary variable to receive the value of timesWork * valueHora and use the command Console.WriteLine("SALARY = U$ " + salario.ToString("F2", CultureInfo.InvariantCulture)); to display the message with points, but the exercise asks that no other variables are created than those requested.

  • When converting the reading, your code can generate an exception. Use Tryparce instead of Parse. int32.Tryparse(Console.Readline(), out numeroFunc);

1 answer

1

Just as you did not need to use another variable to use the result of (horasTrabalhadas * valorHora) in the method Console.WriteLine(), also does not need it in order to use the method ToString().

Do so:

Console.WriteLine("SALARY = U$ " + (horasTrabalhadas * valorHora).ToString("F2", CultureInfo.InvariantCulture));

Whichever expression results in a value(its result).
Every value has an associated type. Even if the expression has not been assigned to a variable it is possible to use such methods directly in the expression.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.