This is a migrated thread and some comments may be shown as answers.

Disable auto round for decimal

11 Answers 1007 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Flavio
Top achievements
Rank 1
Flavio asked on 27 Mar 2012, 09:15 PM
Hi,

  I updated my applications to new version, Telerik 2012 Q1 but before I was using Telerik 2012. When I was testing the application I saw a grid doesn't display the value of the previous version of Telerik. When I set a value 12.379 the value is rounded to 12.38 and in the previous version I set a value 12.379 and it keeps the value.
 I'd like know how to do disable the round option when I set values like 12.379?

Example attached.

Thanks!

11 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 30 Mar 2012, 03:42 PM
Hello Flavio,

Thank you for writing.

GridViewDecimalColumn has a property called DecimalPlaces which determines how many decimals should be displayed when the cell is edited. Additionally, there is a FormatString property of the column, which is used to format the cells when they are not in edit mode:
((GridViewDecimalColumn)radGridView1.Columns["MyColumnName"]).DecimalPlaces = 5;
((GridViewDecimalColumn)radGridView1.Columns["MyColumnName"]).FormatString = "{0:#,0.0000}";

I hope this helps.

Regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Alejandro
Top achievements
Rank 1
answered on 01 Oct 2012, 11:24 PM
it does not work ... any idea?? is still auto rounding
0
Stefan
Telerik team
answered on 04 Oct 2012, 02:09 PM
Hi Alejandro,

Thank you for writing.

You need to set the DecimalPlaces and the FormatString of the column with the number of decimal places desired. For example in my previous post I am allowing up to 5 decimal places, so any extra one will be rounded.

I hope this helps.
 
Kind regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Aaron
Top achievements
Rank 2
answered on 20 Jun 2013, 03:57 PM
The solution you are proposing is not flawless, because if you put a number like 28.9999999999999 will get round to 29, so it is not good if your data needs to be accurate and without rounding.
0
Stefan
Telerik team
answered on 25 Jun 2013, 08:27 AM
Hi Aaron,

Depending on your requirements, you can set the corresponding DecimalPlaces and FormatString. If you do not want rounding just set it the desired DecimalPlaces and FormatString:
((GridViewDecimalColumn)radGridView1.Columns["DecimalColumn"]).DecimalPlaces = 5;
((GridViewDecimalColumn)radGridView1.Columns["DecimalColumn"]).FormatString = "{0:N5}";

Make sure you column DataType supports decimals as well i.e. decimal, double.

Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
0
Saif
Top achievements
Rank 2
answered on 13 Jan 2016, 06:12 AM
I'm having the same problem, even if i apply the decimalplaces and formatstring the value that i entered is still rounding
0
Stefan
Telerik team
answered on 15 Jan 2016, 07:37 AM
Hello Saif,

Can you please provide an example - what value are you entering, and also what column type you use and the settings in the DecimalPlaces and the FormatString properties?

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Mary
Top achievements
Rank 1
answered on 03 Sep 2018, 04:21 PM
Could it be that the data type of your underlying DataSource column is Int rather than Decimal? The reason that my decimal columns were rounding up (in spite of setting the DecimalPlaces and FormatString properties) was because the columns in my DataTable were Int32.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Sep 2018, 08:10 AM
Hello, Mary,         

For numeric fields RadGridView offers GridViewDecimalColumn. You can specify the DataType, DecimalPlaces and FormatString properties of the GridViewDecimalColumn. Thus, the value will be integer and it is not expected to be rounded. If you are still experiencing any undesired behavior it would be greatly appreciated if you can provide a sample code snippet with which I can replicate the problem on my end and make an adequate analysis of the precise case.

I hope this information helps. If you have any additional questions, please let me know.  
 
Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
F3M
Top achievements
Rank 2
answered on 21 Feb 2020, 05:53 PM
I will use this post because, I'm having the same problem, even if i apply the decimalplaces and formatstring the value that i entered is still rounding
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Feb 2020, 12:15 PM
Hello, 

Note that even though you may format a GridViewDecimalColumn to display 2 decimal places, the user is allowed to enter more digits. Hence, the value will be rounded up to 2 decimal places. If your requirement is to restrict the user to a specific number of decimal places, you may consider using a GridViewMaskBoxColumn with a numeric mask. I have prepared a sample code snippet for better illustration how to setup two GridViewMaskBoxColumns with integer and decimal values:
        public RadForm1()
        {
            InitializeComponent();

            List<Item> items = new List<Item>();
            for (int i = 0; i < 10; i++)
            {
                items.Add(new Item(i,"Item" + i, i * 1.33m));
            }
            this.radGridView1.DataSource = items;
            this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
            
            GridViewDecimalColumn idColumn = this.radGridView1.Columns["Id"] as GridViewDecimalColumn;
            idColumn.DataType = typeof(int);
            idColumn.DecimalPlaces = 0;
            idColumn.FormatString = "{0:N0}";

            GridViewDecimalColumn priceColumn = this.radGridView1.Columns["Price"] as GridViewDecimalColumn;
            priceColumn.DataType = typeof(decimal);
            priceColumn.DecimalPlaces = 2;
            priceColumn.FormatString = "{0:N2}";

            GridViewMaskBoxColumn maskBoxColumn = new GridViewMaskBoxColumn("Masked_Id"); 
            maskBoxColumn.FieldName = "Id"; 
            maskBoxColumn.MaskType = MaskType.Numeric;
            maskBoxColumn.Mask = "N0"; 
            maskBoxColumn.FormatString = "{0:N0}";
            maskBoxColumn.DataType = typeof(int);
            radGridView1.MasterTemplate.Columns.Add(maskBoxColumn);

            maskBoxColumn = new GridViewMaskBoxColumn("Masked_Price");
            maskBoxColumn.FieldName = "Price";
            maskBoxColumn.MaskType = MaskType.Numeric;
            maskBoxColumn.Mask = "N2";
            maskBoxColumn.FormatString = "{0:N2}";
            maskBoxColumn.DataType = typeof(decimal);
            radGridView1.MasterTemplate.Columns.Add(maskBoxColumn);

            this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
        }

        private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            GridSpinEditor spinEditor = e.ActiveEditor as GridSpinEditor;
            if (spinEditor != null)
            {
                RadSpinEditorElement el = spinEditor.EditorElement as RadSpinEditorElement;
                if (this.radGridView1.CurrentColumn.Name == "Id")
                {
                    el.DecimalPlaces = 0;
                }
                else
                {
                    el.DecimalPlaces = 2;
                }
            }
        }

        public class Item
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public decimal Price { get; set; }

            public Item(int id, string name, decimal price)
            {
                this.Id = id;
                this.Name = name;
                this.Price = price;
            }
        }
Please give the sample project a try and see how it works for your case. 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Flavio
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Alejandro
Top achievements
Rank 1
Aaron
Top achievements
Rank 2
Saif
Top achievements
Rank 2
Mary
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
F3M
Top achievements
Rank 2
Share this question
or