1
I need to do a percentage calculation, but I’m not getting the exact amount.
Example:
int valorUni = 8;
int valorTotal = 116;
double result;
result = (valorUni / valorTotal) * 100;
//Resultado esperado: 7,547169811320755
//Resultado que saí: 7
1
I need to do a percentage calculation, but I’m not getting the exact amount.
Example:
int valorUni = 8;
int valorTotal = 116;
double result;
result = (valorUni / valorTotal) * 100;
//Resultado esperado: 7,547169811320755
//Resultado que saí: 7
4
Making a cast:
using static System.Console;
public class Program {
public static void Main() {
int valorUni = 8;
int valorTotal = 116;
double result = ((double)valorUni / valorTotal) * 100;
WriteLine(result);
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
If you order to split two integers the result can only be integer, then you need to transform at least one of them into double so that the result has decimal places.
But note that it seems that you are working with monetary value. double should not be used for monetary value. See What is the correct way to use the float, double and decimal types?.
Anyway it is not to give the result that is waiting.
Browser other questions tagged c# .net typing
You are not signed in. Login or sign up in order to post.