C# percentage is 0

前端 未结 6 1204
囚心锁ツ
囚心锁ツ 2021-01-22 08:35

I have a problem when I try to solve simple equation. My equation is find the percentage of student marks. This is my code:

using System;
using System.Collection         


        
相关标签:
6条回答
  • 2021-01-22 09:13

    The type of a result depends on the variables that are used.

    Any mathematical operation between 2 ints will return an int.
    You need to cast one of them to a decimal before doing the mathematical operation:

     res = (oneee / (decimal)two) * 10;
    
    0 讨论(0)
  • 2021-01-22 09:14

    try this:

    private void button1_Click(object sender, EventArgs e)
            {
                decimal oneee = 300;
                decimal two = 1000;
                decimal thee = 10;
                decimal aResult = (oneee / two) * 10;
                label1.Text = aResult.ToString("0.00");
            }
    
    0 讨论(0)
  • 2021-01-22 09:14

    You're performing division between two integers - which means the result is computed as an integer too. 300 / 1000 is 0... and then you multiply that by 10, which is still 0.

    Options:

    • Use floating point arithmetic. You could do this by making all the variables double, or by casting:

      ret = (int) ((oneee / (double) two) * 10);

      (Other floating point types would work too, such as decimal or float... They may give very slightly different results in some cases.)

    • Multiply by 10 first:

      res = (oneee * 10) / two;
      

    Note that for a percentage, you'd want to multiply by 100, not 10. Multiplying first is probably simpler than using floating point if you only want an integer result, and if you know that the multiplication will never overflow.

    Also note that to experiment with things like this quickly, it's much simpler to use a console app than Windows Forms. Here's a complete example showing it working:

    using System;
    class Test
    {
        static void Main()
        {
            int oneee = 300;
            int two = 1000;
            int res = (oneee * 10) / two;
            Console.WriteLine(res); // 3        
        }
    }
    

    EDIT: If you were intending to use the p format specifier, you should be using a floating point number instead:

    int marksScored = 300;
    int marksAvailable = 1000;
    double proportion = ((double) marksScored) / marksAvailable;
    string text = proportion.ToString("p1"); // "30.0%"
    

    Note that only one operand of the division operator needs to be a double - although you could cast both of them if you find that clearer.

    0 讨论(0)
  • 2021-01-22 09:16

    In all C based programming languages (like C#) the operation will be evaluated based on the highest type used in the expression. So if you will multiply two integers the compiler will use integer multiplication, but if you multiply a float and an integer the compiler will use float multiplication. In your case you divide two integers so the compiler will use integer division.

    To solve your problem you should transform one of the numbers into a float. You can do this in many ways. Here are a few:

    res = ((float) oneee / two) * 10;
    res = ((double) oneee / two) * 10;
    
    0 讨论(0)
  • 2021-01-22 09:18

    you are working with integers so the result is also integer and 0.3 becomes 0. You should use double instead or cast to double before calculation.

    0 讨论(0)
  • 2021-01-22 09:20
    you are using int and when you are dividing it by 
    
    (1000/300) it will result 0.3. But the data type is int, so result is 0;
    
    Please use below code
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace thered_Try
    {
        public partial class Form1 : Form
        {
            decimal res =0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                decimal oneee = 300;
                decimal two = 1000;
                decimal thee = 10;
                res = (oneee / two) * 10;
                label1.Text = res.ToString();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form2 form = new Form2();
                form.ShowDialog();
            }
        }
    }
    
    Thanks, Let me know your result. 
    
    0 讨论(0)
提交回复
热议问题