Display product result of two variables with periods instead of commas (C #)

0

I am solving an exercise where it is necessary to state the number of an employee, how many hours he works and how much he receives per hour, then the system should display the number of this employee and his salary, the problem is that the result should be displayed with dots instead of commas 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 that I could solve the problem by creating a wage variable to receive the value of working hours * time value and using the Console.WriteLine("SALARY = U$ " + salario.ToString("F2", CultureInfo.InvariantCulture)); command to display the dotted message, but the exercise asks that no variables other than the requested variables be created .

    
asked by anonymous 06.10.2018 / 17:15

1 answer

1

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

Do this:

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

Any expression results in a value (the your result). The entire value has an associated type. Even if the expression has not been assigned to a variable it is possible to use methods of this type, directly in the expression.

    
06.10.2018 / 19:07