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.
c#
public class Student
{

private int totalmarks;

public string? Name { get; set; }

public int Marks { get; set; }

public int TotalMarks
{
get
{
return (this.Marks / 250) * 100;
}
}

}

Student student = new Student();

student.Marks = 120;

int gba = student.TotalMarks;

Console.WriteLine(gba);
c#
public class Student
{

private int totalmarks;

public string? Name { get; set; }

public int Marks { get; set; }

public int TotalMarks
{
get
{
return (this.Marks / 250) * 100;
}
}

}

Student student = new Student();

student.Marks = 120;

int gba = student.TotalMarks;

Console.WriteLine(gba);
45 Replies
SG97
SG972mo ago
Don't use an int You could try decimal
steven preadly
steven preadly2mo ago
for the total marks or for the marks
SG97
SG972mo ago
Total
Angius
Angius2mo ago
And make sure to make at least one part of the equation a decimal
SG97
SG972mo ago
Ah yes
Angius
Angius2mo ago
public decimal TotalMarks => Marks / 250m * 100;
public decimal TotalMarks => Marks / 250m * 100;
for example
Mayor McCheese
Mayor McCheese2mo ago
do you understand why you're getting zero?
steven preadly
steven preadly2mo ago
i may have done somthing wrong in the equation itself let me check
Angius
Angius2mo ago
The issue is integer division
Mayor McCheese
Mayor McCheese2mo ago
it's not that, essentialy integers can't carry a fractional number
SG97
SG972mo ago
It's not the equation per se
MODiX
MODiX2mo ago
Angius
REPL Result: Success
7 / 250
7 / 250
Result: int
0
0
Compile: 145.894ms | Execution: 16.908ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese2mo ago
so the nearest integer is in fact 0
Angius
Angius2mo ago
Meanwhile
MODiX
MODiX2mo ago
Angius
REPL Result: Success
7 / 250m
7 / 250m
Result: decimal
0.028
0.028
Compile: 269.572ms | Execution: 18.151ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese2mo ago
but also...
MODiX
MODiX2mo ago
Mayor McCheese
REPL Result: Success
250/7
250/7
Result: int
35
35
Compile: 183.660ms | Execution: 15.366ms | React with ❌ to remove this embed.
steven preadly
steven preadly2mo ago
what dose the m means
Mayor McCheese
Mayor McCheese2mo ago
it 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
steven preadly
steven preadly2mo ago
where can i read about this modifiers
Mayor McCheese
Mayor McCheese2mo ago
decimal d = 2.0 iirc is a syntax error.
MODiX
MODiX2mo ago
Mayor McCheese
REPL Result: Failure
decimal d = 2.0;
decimal d = 2.0;
Exception: CompilationErrorException
- Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type
- Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type
Compile: 226.024ms | Execution: 0.000ms | React with ❌ to remove this embed.
steven preadly
steven preadly2mo ago
i re-created my code as below
c#

public class Student
{
private int _marks;

private float totalmarks;

public string? Name { get; set; }

public int Marks
{
set
{
_marks = value;
}
get
{
return _marks;
}
}

public float TotalMarks
{
get
{
return ( _marks / 250f) * 100;
}
}

}
c#

public class Student
{
private int _marks;

private float totalmarks;

public string? Name { get; set; }

public int Marks
{
set
{
_marks = value;
}
get
{
return _marks;
}
}

public float TotalMarks
{
get
{
return ( _marks / 250f) * 100;
}
}

}
Angius
Angius2mo ago
private int _marks;
// ...
public int Marks
{
set
{
_marks = value;
}
get
{
return _marks;
}
}
private int _marks;
// ...
public int Marks
{
set
{
_marks = value;
}
get
{
return _marks;
}
}
can just be
public int Marks { get; set; }
public int Marks { get; set; }
btw
Mayor McCheese
Mayor McCheese2mo ago
in most cases autoprops are fine, some older frameworks it doesn't work as well.
steven preadly
steven preadly2mo ago
and this means that i hcve to use this suffiex every time i am using float decimal or double
Mayor McCheese
Mayor McCheese2mo ago
it's best to use the suffix for a bare number imho. but it does depend on the application.
steven preadly
steven preadly2mo ago
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
Angius
Angius2mo ago
$getsetdevolve
MODiX
MODiX2mo ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
steven preadly
steven preadly2mo ago
@Right Dr. Mayor McCheese Esq. in that code the set the prop to float while it returns ineger 48 thats looks weried to me
c#

public float TotalMarks
{
//the get returns integer
get
{
return (Marks / 250f) * 100f;
}
}

}
c#

public float TotalMarks
{
//the get returns integer
get
{
return (Marks / 250f) * 100f;
}
}

}
Angius
Angius2mo ago
This will return a float
steven preadly
steven preadly2mo ago
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 remender
Angius
Angius2mo ago
You'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 anywhere
steven preadly
steven preadly2mo ago
execuse my stupidity do you mean that that part will be converted to decimal
(Marks / 250f)
(Marks / 250f)
Angius
Angius2mo ago
To a float Because of the f suffix
steven preadly
steven preadly2mo ago
and 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 pression
Angius
Angius2mo ago
Yes Since integers cannot represent decimal values
steven preadly
steven preadly2mo ago
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
Angius
Angius2mo ago
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:
Marks / 250m
(decimal)Marks / 250m
Marks * 1m / 250
Marks / (decimal)250
Marks / 250m
(decimal)Marks / 250m
Marks * 1m / 250
Marks / (decimal)250
steven preadly
steven preadly2mo ago
ok thank you very mush
Mayor McCheese
Mayor McCheese2mo ago
sorry I was cooking and not paying attention.
steven preadly
steven preadly2mo ago
no problem