Disable AutoRound DecimalColum Expression

2 posts, 0 answers
  1. Sebastian
    Sebastian avatar
    1 posts
    Member since:
    Jan 2018

    Posted 22 Jan 2018 Link to this post

    Hi Team Telerik,

             I have a issue with the expression in Telerik winforms R3 2017 SP1. When i declared 2 columns type of decimal and i need show the result in another column with 8 decimals (0,00000000 or {"0:N8"}), this result auto round de last decimal to next value. Please, can you tell me how can i fix that.

             I attach an image of the problem.

    Thanks!

     

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3282 posts

    Posted 23 Jan 2018 Link to this post

    Hello, Sebastian,

    Thank you for writing.  

    DecimalPlaces and FormatString properties ensure exactly how many decimal places should be shown in the spin editor and in the cell respectively. This means that if you have less decimal places than the value, they will be complemented. In case of greater number of decimal places, cell value will be rounded. If you want to prevent rounding when exceeding the decimal places and just cut off the exceeding digits, you are able to use CellFormatting event in order to manipulate e.CellElement.Text and e.CellElement.Value as you wish. Here is a sample code snippet:
    public RadForm1()
        {
            InitializeComponent();
     
            this.radGridView1.CellFormatting += radGridView1_CellFormatting;
            GridViewDecimalColumn col = new GridViewDecimalColumn("Numeric");
            this.radGridView1.Columns.Add(col);
            col.DecimalPlaces = 8;
            this.radGridView1.Rows.Add(16.666666676666);
     
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }
     
        int maxDecimalPlaces = 8;
     
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.Name == "Numeric" && e.RowIndex > -1)
            {
                string text = null;
                int decimalPointIndex = e.CellElement.Text.IndexOf('.');
                if (decimalPointIndex > -1 && (e.CellElement.Text.Length - 1 - decimalPointIndex > maxDecimalPlaces))
                {
                    text = e.CellElement.Text.Substring(0, decimalPointIndex + maxDecimalPlaces + 1);
                    e.CellElement.Value = decimal.Parse(text);
                }
            }
        }


    Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

    I hope this information helps. Should you have further questions I would be glad to help. 
     
     Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top