# 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
SG973mo ago
Don't use an `int` You could try `decimal`
for the total marks or for the marks
SG973mo ago
Total
Angius3mo ago
And make sure to make at least one part of the equation a decimal
SG973mo ago
Ah yes
Angius3mo ago
``public decimal TotalMarks => Marks / 250m * 100;``
``public decimal TotalMarks => Marks / 250m * 100;``
for example
Mayor McCheese3mo ago
do you understand why you're getting zero?
i may have done somthing wrong in the equation itself let me check
Angius3mo ago
The issue is integer division
Mayor McCheese3mo ago
it's not that, essentialy integers can't carry a fractional number
SG973mo ago
It's not the equation per se
MODiX3mo 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 McCheese3mo ago
so the nearest integer is in fact 0
Angius3mo ago
Meanwhile
MODiX3mo 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 McCheese3mo ago
but also...
MODiX3mo 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.
what dose the `m` means
Mayor McCheese3mo 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
Mayor McCheese3mo ago
decimal d = 2.0 iirc is a syntax error.
MODiX3mo 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.
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;        }    }}``
Angius3mo 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 McCheese3mo ago
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
Mayor McCheese3mo ago
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
Angius3mo ago
\$getsetdevolve
MODiX3mo 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; }}``
@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;        }    }   }``
Angius3mo ago
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 remender
Angius3mo 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
execuse my stupidity do you mean that that part will be converted to decimal
``(Marks / 250f)``
``(Marks / 250f)``
Angius3mo ago
To a float Because of the `f` suffix
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
Angius3mo ago
Yes Since integers cannot represent decimal values
``Marks / 250m(decimal)Marks / 250mMarks * 1m / 250Marks / (decimal)250``
``Marks / 250m(decimal)Marks / 250mMarks * 1m / 250Marks / (decimal)250``