none
decimal convert to integer c# RRS feed

  • Question

  • good morning programmers I have a form made in visual studio 2015 and sql server 2014 in the form where I need to round the results

    this code that I have one is 5%, 7%, 10%

    intoces in total to pay I need that it leaves rounded the discount and total to pay for example in the discount of seven% it leaves result 103.6 it should be 104

    $5,543 ie $ 1 to $ 5 is rounded down will be charged the price of the previous decade =$5,540

    $5.546 and from $ 6 to $ 9 is surrendered to arriaba will be charged the price of the top 5,550

    private void CalcularTotalCaja()
            {
                var total = Detalles.Sum(x => x.Importe);
                decimal descuentocinco = 0;
                decimal desucentosiete = 0;
                decimal descuentodiez = 0;
               
              
                    if (RbPorcentajecinco.Checked == true)
                    {
                        descuentocinco = Math.Round(Convert.ToDecimal(total * 0.05M), 2);//5%
                        TxtDescuento.Text = descuentocinco.ToString("N2");
                    }
    
    
                    else if (RbPorcentajesiete.Checked == true) 
               
                    
                    {
                        desucentosiete = Math.Round(Convert.ToDecimal(total * 0.07M), 2); //7%
                        TxtDescuento.Text = desucentosiete.ToString("N2");
                    }
    
      
    
                    else if (RbPorcentajediez.Checked == true)
                    {
                        descuentodiez = Math.Round(Convert.ToDecimal(total * 0.1M), 2);//10% 
                        TxtDescuento.Text = descuentodiez.ToString("N2");
                    
                    }
    
               
                    else if (RbSindescuento.Checked == true) without discount
                    {
                        TxtDescuento.Clear();
                        TxtDescuento.Text = "0";
                       
                        LblTotalVenta.Text = total.ToString("#,##0.00");
                        LblTotalPagar.Text = total.ToString("#,##0.00");
                    }
    
                var dscto = Math.Round(Convert.ToDecimal(TxtDescuento.Text)) ;
               
               
                var montopagar = (total - dscto);
                LblTotalPagar.Text = montopagar.ToString("#,##0.00");
               
            }

    Friday, June 14, 2019 4:02 PM

Answers

  • You're making it harder than it needs to be.  If you want the discount amount rounded to even dollars/pesos, then don't round it to two places.  To round to the tens place, just divide by 10, round, then multiply by 10.  Thus:

    private void CalcularTotalCaja()
    {
        var total = Detalles.Sum(x => x.Importe);
        decimal descuento = 0;
    
        if (RbPorcentajecinco.Checked)
        {
            descuento = 0.05;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.07;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.10;
        }
    
        var dscto = Math.Round(total * descuento);
        var montopagar = Math.Round((total - dscto) / 10.0) * 10.0;
    
        LblTotalVenta.Text = total.ToString("#,##0.00");
        TxtDescuento.Text = dscto.ToString("N2");
        LblTotalPagar.Text = montopagar.ToString("#,##0.00");
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by djnilo Saturday, June 15, 2019 5:00 AM
    Friday, June 14, 2019 5:52 PM

  • but I have an error in this line


    Did you try this?

    var montopagar = Math.Round((total - dscto) / 10.0M) * 10.0M;
    

    - Wayne

    Thank you very much for answering, Wayne. But there is no problem in this line, it is not calculated in 10% you help me

     private void CalcularTotalCaja()
            {
                var total = Detalles.Sum(x => x.Importe);
                decimal descuento = 0;
              
    
                if (RbPorcentajecinco.Checked)
                {
                    descuento = 0.05M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.07M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.10M;//does not calculate
                }
    
                var dscto = Math.Round(total * descuento);
                var montopagar = Math.Round((total - dscto) / 10.0M) * 10.0M;
    
                LblTotalVenta.Text = total.ToString("#,##0.00");
                TxtDescuento.Text = dscto.ToString("N2");
                LblTotalPagar.Text = montopagar.ToString("#,##0.00");
            }

    • Marked as answer by djnilo Saturday, June 15, 2019 4:59 AM
    Saturday, June 15, 2019 4:34 AM

All replies

  • You're making it harder than it needs to be.  If you want the discount amount rounded to even dollars/pesos, then don't round it to two places.  To round to the tens place, just divide by 10, round, then multiply by 10.  Thus:

    private void CalcularTotalCaja()
    {
        var total = Detalles.Sum(x => x.Importe);
        decimal descuento = 0;
    
        if (RbPorcentajecinco.Checked)
        {
            descuento = 0.05;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.07;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.10;
        }
    
        var dscto = Math.Round(total * descuento);
        var montopagar = Math.Round((total - dscto) / 10.0) * 10.0;
    
        LblTotalVenta.Text = total.ToString("#,##0.00");
        TxtDescuento.Text = dscto.ToString("N2");
        LblTotalPagar.Text = montopagar.ToString("#,##0.00");
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by djnilo Saturday, June 15, 2019 5:00 AM
    Friday, June 14, 2019 5:52 PM
  • Maybe I don't understand what you're asking but Math.Round already does the rounding that you asked for.

    //Returns 5.54 because you want 2 decimals of precision and 3rd digit is < 5
    Math.Round(5.543, 2) 
    
    //Returns 5.55
    Math.Round(5.546, 2)

    If you then want the 3rd digit of precision in your output then specify the precision as part of the conversion to string.

    value.ToString(".000")
    In your example you mixed commas and decimals and that would be 2 different concepts altogether. Rounding decimals works outside the box. If you want instead to ensure that whole numbers are rounded to tenths (e.g. 456 = 460) then you'll need to divide by the part you want and then do rounding and then convert back. Somebody already posted how to do this so I'll just link to that. Do the division, then round and then multiple back.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 14, 2019 6:02 PM
    Moderator
  • You're making it harder than it needs to be.  If you want the discount amount rounded to even dollars/pesos, then don't round it to two places.  To round to the tens place, just divide by 10, round, then multiply by 10.  Thus:

    private void CalcularTotalCaja()
    {
        var total = Detalles.Sum(x => x.Importe);
        decimal descuento = 0;
    
        if (RbPorcentajecinco.Checked)
        {
            descuento = 0.05;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.07;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.10;
        }
    
        var dscto = Math.Round(total * descuento);
        var montopagar = Math.Round((total - dscto) / 10.0) * 10.0;
    
        LblTotalVenta.Text = total.ToString("#,##0.00");
        TxtDescuento.Text = dscto.ToString("N2");
        LblTotalPagar.Text = montopagar.ToString("#,##0.00");
    }


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Many thanks for responding so quickly
    Tim Roberts
    but I have an error in this line

    private void CalcularTotalCaja()
            {
                var total = Detalles.Sum(x => x.Importe);
                decimal descuento = 0;
    
                if (RbPorcentajecinco.Checked)
                {
                    descuento = 0.05M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.07M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.10M;
                }
    
                var dscto = Math.Round(total * descuento);
                var montopagar = Math.Round((total - dscto) / 10.0) * 10.0;
    
                LblTotalVenta.Text = total.ToString("#,##0.00");
                TxtDescuento.Text = dscto.ToString("N2");
                LblTotalPagar.Text = montopagar.ToString("#,##0.00");
            }

    Friday, June 14, 2019 8:27 PM

  • but I have an error in this line


    Did you try this?

    var montopagar = Math.Round((total - dscto) / 10.0M) * 10.0M;
    
    

    - Wayne

    Friday, June 14, 2019 10:39 PM

  • but I have an error in this line


    Did you try this?

    var montopagar = Math.Round((total - dscto) / 10.0M) * 10.0M;
    

    - Wayne

    Thank you very much for answering, Wayne. But there is no problem in this line, it is not calculated in 10% you help me

     private void CalcularTotalCaja()
            {
                var total = Detalles.Sum(x => x.Importe);
                decimal descuento = 0;
              
    
                if (RbPorcentajecinco.Checked)
                {
                    descuento = 0.05M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.07M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.10M;//does not calculate
                }
    
                var dscto = Math.Round(total * descuento);
                var montopagar = Math.Round((total - dscto) / 10.0M) * 10.0M;
    
                LblTotalVenta.Text = total.ToString("#,##0.00");
                TxtDescuento.Text = dscto.ToString("N2");
                LblTotalPagar.Text = montopagar.ToString("#,##0.00");
            }

    • Marked as answer by djnilo Saturday, June 15, 2019 4:59 AM
    Saturday, June 15, 2019 4:34 AM
  •             else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.07M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.10M;//does not calculate
                }
    
    

    Fix your logic. 

    Both if() statements test the same item: RbPorcentajesiete.Checked

    - Wayne

    Saturday, June 15, 2019 4:50 AM

  • 
                if (RbPorcentajecinco.Checked)
                {
                    descuento = 0.05M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.07M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.10M;//does not calculate
                }
    
    

    I suspect that the third if() should be:

     //else if (RbPorcentajesiete.Checked)
     else if (RbPorcentajediez.Checked)
     {
         descuento = 0.10M;
     }
    

    - Wayne

    Saturday, June 15, 2019 4:58 AM
  •             else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.07M;
                }
                else if (RbPorcentajesiete.Checked)
                {
                    descuento = 0.10M;//does not calculate
                }
    

    Fix your logic. 

    Both if() statements test the same item: RbPorcentajesiete.Checked

    - Wayne

    thank you very much Wayne you were right jajja
    Saturday, June 15, 2019 4:59 AM
  • You're making it harder than it needs to be.  If you want the discount amount rounded to even dollars/pesos, then don't round it to two places.  To round to the tens place, just divide by 10, round, then multiply by 10.  Thus:

    private void CalcularTotalCaja()
    {
        var total = Detalles.Sum(x => x.Importe);
        decimal descuento = 0;
    
        if (RbPorcentajecinco.Checked)
        {
            descuento = 0.05;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.07;
        }
        else if (RbPorcentajesiete.Checked) 
        {
            descuento = 0.10;
        }
    
        var dscto = Math.Round(total * descuento);
        var montopagar = Math.Round((total - dscto) / 10.0) * 10.0;
    
        LblTotalVenta.Text = total.ToString("#,##0.00");
        TxtDescuento.Text = dscto.ToString("N2");
        LblTotalPagar.Text = montopagar.ToString("#,##0.00");
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    thank you very much Tim Roberts excellent thanks for your time
    thank you very much Tim Roberts excellent thanks for your time
    Saturday, June 15, 2019 5:01 AM
  • Maybe I don't understand what you're asking but Math.Round already does the rounding that you asked for.

    //Returns 5.54 because you want 2 decimals of precision and 3rd digit is < 5
    Math.Round(5.543, 2) 
    
    //Returns 5.55
    Math.Round(5.546, 2)

    If you then want the 3rd digit of precision in your output then specify the precision as part of the conversion to string.

    value.ToString(".000")
    In your example you mixed commas and decimals and that would be 2 different concepts altogether. Rounding decimals works outside the box. If you want instead to ensure that whole numbers are rounded to tenths (e.g. 456 = 460) then you'll need to divide by the part you want and then do rounding and then convert back. Somebody already posted how to do this so I'll just link to that. Do the division, then round and then multiple back.


    Michael Taylor http://www.michaeltaylorp3.net

    thank you very mach CoolDadTx
    Saturday, June 15, 2019 5:03 AM