How to create this expression in C#

Asked

Viewed 437 times

0

At first, the program I made the calculation using the following expression:

valorComDescontos = valorTotal - (valorTotal * percentual);

Where the valorTotal is a value assigned by the user (a variable), the percentual is a fixed value (0.0229) and the valorComDescontos is the amount that will be calculated with the INTEREST (valorTotal* percentual). What I am trying to calculate now is the following: the user will enter a value and the program will calculate as a result the value having the discounts will have the value entered by the user. Changing the calculation, I came to the following expression:

valorTotal = valorComDescontos + (valorTotal * percentual)

But as the variable I will not have value is the valorTotal, it could not be on both sides of the expression. How to calculate this?

Example (Previously):

valorTotal = 1000; #valor que o usuário inseriu
percentual = 0.0229; #valor FIXO

valorComDescontos = 1000 - (1000 * 0.0229);
valorComDescontos = 1000 - 22,9;
valorComDescontos = 977,1;

Example (How to be calculated now):

valorComDescontos = 1000; #valor que seria dado pelo usuário
percentual = 0.0229; #valor FIXO

valorTotal = 1023,44; #seria esse o valor aproximado, onde descontando o percentual, chegaria ao valorComDescontos
  • For mathematical calculations I use this framework https://ncalc.codeplex.com/ Give a look, it is very good and I believe it meets your need

1 answer

1


Basic mathematics:

valorComDescontos = valorTotal - (valorTotal * percentual)

You will have the values of valorComDescontos and percentual, seeking the value of valorTotal. First, put in evidence on the right side of equality:

valorComDescontos = valorTotal * (1 - percentual)

Now, if you divide both sides by 1 - percentual, one has:

valorComDescontos / (1 - percentual) = valorTotal

That is to say:

valorTotal = valorComDescontos / (1 - percentual)

Example:

valorComDescontos = 1000,00
percentual = 0,0229

valotTotal = 1000 / (1 - 0,0229)
valorTotal = 1000 / 0,9771
valorTotal = 1023,4367
valorTotal ~ 1023,44

As expected.

  • Thank you very much, that’s what I was trying to do

Browser other questions tagged

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