Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to round value to only 3 decimal points?

i am working on a inventory software and was calculating the discount by percentage and amount as well.

  • means when i enter % amount it automatically convert to price#
  • and when i enter price it automatically converts to the % #

the code i am using to convert price in % is this is:

if(txtDiscount.Text == "" || txtGrandTotal.Text == "")
            {
                MessageBox.Show("Please Enter amount in Grand Total",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
              //  double percent;
                double grandTotal;
                double per;
                double txDiscount;

                //percent = Convert.ToInt32(txtPercant.Text);
                grandTotal = Convert.ToInt32(txtGrandTotal.Text);
                txDiscount = Convert.ToInt32(txtDiscount.Text);


                per = txDiscount / grandTotal * 100;

                //per = percent / 100 * grandTotal;

                if (per % 3 != 0)
                    per = (per - per % 3) + 3;


                txtPercant.Text = Convert.ToString(per);

                    ................
                    ................
                    ................
                    ................
                    ................

now let's suppose if i am entering the amount is "25", then the percentage it count is "2.77777777777778". i want to limit this to three decimals after point, like i want to to be "2.778" or whatever means i want to to show only 3 decimals after point, can any one help me out in which way i can do it ?? any suggestions would be appreciated!!

EDIT:

Coversion from Price to % Code is:

  private void txtDiscount_Leave(object sender, EventArgs e)
        {  
            if(txtDiscount.Text == "" || txtGrandTotal.Text == "")
            {
                MessageBox.Show("Please Enter amount in Grand Total",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
              //  double percent;
                double grandTotal;
                double per;
                double txDiscount;

                //percent = Convert.ToInt32(txtPercant.Text);
                grandTotal = Convert.ToInt32(txtGrandTotal.Text);
                txDiscount = Convert.ToInt32(txtDiscount.Text);


                per = txDiscount / grandTotal * 100;

                //per =  Math.Round(per, 3);
                per = Math.Truncate(per * 1000) / 1000;

                txtPercant.Text = Convert.ToString(per);








                double lblgrandTotal = 0.0;
                double Disconunt = 0.0;
                double netTotal = 0.0;

                lblgrandTotal = Convert.ToDouble(txtGrandTotal.Text);
                Disconunt = Convert.ToDouble(txtDiscount.Text);
                netTotal = grandTotal - Disconunt;

            //netTotal = Convert.ToInt32(grandTotal) - Convert.ToInt32(Discount);

            lblNetTotal.Text = Convert.ToString(netTotal);

            }
        }

Cobersion from % to Price Code is :

   private void textBox1_Leave(object sender, EventArgs e)
        {
            if (txtPercant.Text == "")
            {
                MessageBox.Show("Please Enter value in Percenent", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                double percent;
                double grandTotal;
                double per;

                percent = Convert.ToDouble(txtPercant.Text);
                grandTotal = Convert.ToDouble(txtGrandTotal.Text);

                per = percent / 100 * grandTotal;

                txtDiscount.Text = Convert.ToString(per);

                double lblgrandTotal = 0.0;
                double Disconunt = 0.0;
                double netTotal = 0.0;

                lblgrandTotal = Convert.ToDouble(txtGrandTotal.Text);
                Disconunt = Convert.ToDouble(txtDiscount.Text);
                netTotal = grandTotal - Disconunt;

                //netTotal = Convert.ToInt32(grandTotal) - Convert.ToInt32(Discount);

                lblNetTotal.Text = Convert.ToString(netTotal);
            }
        }

but the problem i am facing is i am converting % to price and Vise Versa, now in case i am adding 12 in Price it says 12.33%, and when i put 12.33% it converts back it to 11.997Price which is not the same ans it should be... it should be exactly 12 not 11.997... really confused in it..!!

NOTE:

by price i mean that i am calculating % discount and price discount on grand total, means if i add 12% then it calculate the price discount of grand total and if i enter the price amount then it calculates the %discount of grand total

like image 788
Ahsan Hussain Avatar asked Jan 25 '14 19:01

Ahsan Hussain


1 Answers

Math.Round(per, 3)

However, you've two logical errors in your approach. You are using double to deal with amounts of money and percentages applied to it.

double is appropriate for use with natural things in the real world, like the length of your nose, or the number of seconds between a celebrity being arrested and a joke about it appearing on Twitter.

Money and percentage discounts are not natural things, but artificial amounts based on particular numbering systems.

Almost all of these are decimal based, except for Mauritanian ouguiyas, Malagasy ariaries, and the Maltese scudo, and several obsolete currencies (like the old British pound, shillings and pence). Of the three used in current times, the first two are base-5 based (and so representable without error in a decimal system) and the last is pegged to the Euro, so there's no need to worry about it.

Now, when we deal with the real world we get problems because our numbering system is in a particular base, and so we get results like 0.33333333333333333333333333333.

With money, we can only get these mistakes with division, because unless we divide, nothing will force a base-limit error upon us.

But double doesn't use decimal internally, it uses binary. Binary has different numbers that result in recurrent digits after the point. E.g. 0.1 and 0.01. Most of the time the rounding errors involved will cancel out, but sometimes they won't.

Hence, when dealing with money, always use decimal rather than double unless you've a very good reason (if you can't explain why the above doesn't apply to your case, then you don't).

The second problem is that since you're doing percentages, you are doing division, and so you will still get some rounding errors.

The solution is to store one more decimal places than you need. E.g. if you want to be accurate to 3 decimal places (as you say) then store 4.

For example, if at some point we divided 12 by 7 and rounded to three points, we'd have 1.714. If we then multiplied by 7 again, we'd have 11.998. If however we'd stored the four-decimal 1.7143 and only displayed 1.714, then multiplying by 7 again would give us 12.0001 which we'd display (rounding to 3 places again) as 12.

We can't avoid rounding errors, but we can work at a higher precision until the latest moment necessary, so the rounding errors are themselves lost to rounding.

like image 92
Jon Hanna Avatar answered Nov 14 '22 20:11

Jon Hanna