C
C#8mo ago
zzz

✅ converting fractions to percentages and getting an output of 0 no matter what?

public static void Percentages()
{
dpPerc = (dpMark / 35) * 100;
obpPerc = (obpMark / 7) * 100;
cpPerc = (cpMark / 100) * 100;
Console.WriteLine(dpPerc);
Console.WriteLine(obpPerc);
Console.WriteLine(cpPerc);
}
public static void Percentages()
{
dpPerc = (dpMark / 35) * 100;
obpPerc = (obpMark / 7) * 100;
cpPerc = (cpMark / 100) * 100;
Console.WriteLine(dpPerc);
Console.WriteLine(obpPerc);
Console.WriteLine(cpPerc);
}
the mark variables were user inputted integers but when i use this method to get percentages, i outputted them to check they were correct and they are always 0?
7 Replies
zzz
zzzOP8mo ago
perc variables are doubles and mark variables are ints
Angius
Angius8mo ago
Because you're doing integer division
MODiX
MODiX8mo ago
Angius
REPL Result: Success
5 / 100
5 / 100
Result: int
0
0
Compile: 137.152ms | Execution: 16.259ms | React with ❌ to remove this embed.
Angius
Angius8mo ago
Cast one of the numbers to a double, for example, or use a double literal
MODiX
MODiX8mo ago
Angius
REPL Result: Success
5 / 100d
5 / 100d
Result: double
0.05
0.05
Compile: 281.412ms | Execution: 16.428ms | React with ❌ to remove this embed.
zzz
zzzOP8mo ago
thank you so much
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server