C
C#10mo 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
zzzOP10mo ago
perc variables are doubles and mark variables are ints
Angius
Angius10mo ago
Because you're doing integer division
MODiX
MODiX10mo 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
Angius10mo ago
Cast one of the numbers to a double, for example, or use a double literal
MODiX
MODiX10mo 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
zzzOP10mo ago
thank you so much
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server