Question regarding Smallest Value.

Kiexus

New member
Joined
Jul 11, 2021
Messages
3
Programming Experience
Beginner
Hi. I'm a beginner of C#.

While doing a practice question, I have a question that bothers me a lot.

I'm making a invoice window that shows some information such as gross invoice, discount, total, max and min of total invoice.

But as see the line 10-11, the part of data form of largest and smallest invoice amount, i don't know why "0m;" doesn't work for smallest invoice and decimal.MaxValue has to be used.

Anyone can explain to me the reason?

I will attach the entire code below.

Thanks alot.

Invoice Total:
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int numberOfInvoice = 0;
        decimal totalOfInvoice = 0m;
        decimal invoiceAverage = 0m;
        decimal largestInvoice = 0m;
        decimal smallestInvoice = decimal.MaxValue;

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
            decimal discountPercent = 0.25m;
            decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
            decimal invoiceTotal = subtotal - discountAmount;

            txtSubtotal.Text = subtotal.ToString("c");
            txtDicountPercent.Text = discountPercent.ToString("p1");
            txtDiscountAmount.Text = discountAmount.ToString("c");
            txtTotal.Text = invoiceTotal.ToString("c");

            numberOfInvoice++;
            totalOfInvoice += invoiceTotal;
            invoiceAverage = totalOfInvoice / numberOfInvoice;
            largestInvoice = Math.Max(largestInvoice, invoiceTotal);
            smallestInvoice = Math.Min(smallestInvoice, invoiceTotal);

            txtNumberOfInvoice.Text = numberOfInvoice.ToString();
            txtTotalOfInvoice.Text = totalOfInvoice.ToString("c");
            txtInvoiceAverage.Text = invoiceAverage.ToString("c");
            txtLargestInvoice.Text = largestInvoice.ToString("c");
            txtSmallestInvoice.Text = smallestInvoice.ToString("c");

            txtEnterSubtotal.Text = "";
            txtEnterSubtotal.Focus();

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            numberOfInvoice = 0;
            totalOfInvoice = 0m;
            invoiceAverage = 0m;
            largestInvoice = 0m;
            smallestInvoice = decimal.MaxValue;

            txtNumberOfInvoice.Text = "";
            txtTotalOfInvoice.Text = "";
            txtInvoiceAverage.Text = "";
            txtLargestInvoice.Text = "";
            txtSmallestInvoice.Text = "";

            txtEnterSubtotal.Focus();


        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
If you had bothered to debug your code then the reason would be obvious. If you initialise smallestInvoice to zero then what do you think will happen here:
C#:
smallestInvoice = Math.Min(smallestInvoice, invoiceTotal);
No value will ever be less than zero so you'll never actually find the smallest value because smallestInvoice will always be zero. You have to use an initial value that is guaranteed to be larger than at least the first value you're testing. Again, if you had debugged your code and watched all the values as you stepped through the code then you would have seen that for yourself.
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
They are a beginner. I highly doubt they know how to debug let alone know what a debugger is...

Use decimal.Zero instead. On line 11, you have set the smallest invoice to the max value. What's the point? You're changing it again on line 29.
I don't think you know what the code is doing. Math.Min returns the smallest value of a set of two numbers.
See : Decimal.MinValue Field (System) and Math.Min Method (System)

To answer your question regarding using 'm'. You can find your question answered on this link : Using 0M instead of 0 for decimal values?

Please take a look at these and learn the information on both links and the links within the pages, as you will need to learn how to debug your code at some point :
And
 

Kiexus

New member
Joined
Jul 11, 2021
Messages
3
Programming Experience
Beginner
If you had bothered to debug your code then the reason would be obvious. If you initialise smallestInvoice to zero then what do you think will happen here:
C#:
smallestInvoice = Math.Min(smallestInvoice, invoiceTotal);
No value will ever be less than zero so you'll never actually find the smallest value because smallestInvoice will always be zero. You have to use an initial value that is guaranteed to be larger than at least the first value you're testing. Again, if you had debugged your code and watched all the values as you stepped through the code then you would have seen that for yourself.
Thanks a lot for your comment. I haven't thought about debugging but I will check out every line of code with it.
 

Kiexus

New member
Joined
Jul 11, 2021
Messages
3
Programming Experience
Beginner
They are a beginner. I highly doubt they know how to debug let alone know what a debugger is...

Use decimal.Zero instead. On line 11, you have set the smallest invoice to the max value. What's the point? You're changing it again on line 29.
I don't think you know what the code is doing. Math.Min returns the smallest value of a set of two numbers.
See : Decimal.MinValue Field (System) and Math.Min Method (System)

To answer your question regarding using 'm'. You can find your question answered on this link : Using 0M instead of 0 for decimal values?

Please take a look at these and learn the information on both links and the links within the pages, as you will need to learn how to debug your code at some point :
And
Oh I understand how max.min/max part works.
I just thought that line 10 should be the same as line 9 but I couldn't get the smallest invoice value when I ran debug. Later when I checked the answer, line 10 and 48 were "decimal.max value" not "0m". I couldn't understand why.
I appreciate your comment. I should dig a lot more as you said with debug.
 
Top Bottom