Calculating Student Grade Book Average (GBA) in Percent - Encountering Zero Value Issue
I'm working on a student class with properties mark and totalMark. My goal is to define the totalMark property to compute the GBA as a percentage. However, every time I implement the calculation, the totalMark value consistently returns 0.
45 Replies
Don't use an
int
You could try decimal
for the total marks or for the marks
Total
And make sure to make at least one part of the equation a decimal
Ah yes
for example
do you understand why you're getting zero?
i may have done somthing wrong in the equation itself let me check
The issue is integer division
it's not that, essentialy integers can't carry a fractional number
It's not the equation per se
Angius
REPL Result: Success
Result: int
Compile: 145.894ms | Execution: 16.908ms | React with ❌ to remove this embed.
so the nearest integer is in fact 0
Meanwhile
Angius
REPL Result: Success
Result: decimal
Compile: 269.572ms | Execution: 18.151ms | React with ❌ to remove this embed.
but also...
Mayor McCheese
REPL Result: Success
Result: int
Compile: 183.660ms | Execution: 15.366ms | React with ❌ to remove this embed.
what dose the
m
meansit means treat as a decimal
bare numbers in c# have some odd behaviors
so...
2.0 is a double,
2.0d is also a double
where can i read about this modifiers
decimal d = 2.0 iirc is a syntax error.
Mayor McCheese
REPL Result: Failure
Exception: CompilationErrorException
Compile: 226.024ms | Execution: 0.000ms | React with ❌ to remove this embed.
Built-in types - C# reference
Learn C# built-in value and reference types
i re-created my code as below
can just be
btw
in most cases autoprops are fine, some older frameworks it doesn't work as well.
and this means that i hcve to use this suffiex every time i am using float decimal or double
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#6453-integer-literals
here's the list of literals
Not always, no
it's best to use the suffix for a bare number imho.
but it does depend on the application.
do you mean by bare numbers is numbers that are not assigend to a varible
and as i think i dont have to create a private prop in here to , as there is no modification or validation that needs to be done in here
$getsetdevolve
can be shortened to
can be shortened to
can be shortened to
can be shortened to
@Right Dr. Mayor McCheese Esq.
in that code the set the prop to float while it returns ineger
48
thats looks weried to me
This will return a float
correct me if i am wrong i am using the
f
in here to make sure that if the value passed if it is not divisivble by the 250 as above it gets the remenderYou're using
f
here to trigger float division instead of integer division
Because Marks
and 250
are both integers
One of them has to become a decimal type in order to get a decimal out
There's nothing about divisibility or remainder
You're not using the modulo operator anywhereexecuse my stupidity do you mean that that part will be converted to decimal
To a float
Because of the
f
suffixand in that part if i made the accessor return int other than float and i removed the
f
suffix its normal to get a 0 because of lost data pressionYes
Since integers cannot represent decimal values
got the point i this case i have to use the suffiex with any calculation that i am not sure if is result will be float ,double or decimal
You decide what the result will be
Integer divided by a double will be a double
Divided by a float will be a float
Etc
Could even just cast the original integer, or both, to the same effect. All of those would work to produce a decimal, for example:
ok thank you very mush
sorry I was cooking and not paying attention.
no problem