Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To format between 2 and 4 decimal places you can use a custom format string. (As provided by Harlam357)</p> <pre><code>txtcDecimal.DefaultCellStyle.Format = "0.00##" </code></pre> <p>I verified it with the following simple console application:</p> <pre><code> double myDouble = 13.1; double myDouble2 = 13.12345; Console.WriteLine(myDouble.ToString("0.00##")); Console.WriteLine(myDouble2.ToString("0.00##")); Console.Read(); </code></pre> <p>The output was:</p> <pre><code>13.10 13.1235 </code></pre> <p>Harlam's answer is clearly correct....</p> <p><strong>UPDATE:</strong> This is how I implemented it in forms:</p> <pre><code> public Form1() { InitializeComponent(); DataTable dt = new DataTable(); dt.Columns.Add("a"); dt.Rows.Add(123.4); dt.Rows.Add(123.45); dt.Rows.Add(123.456); dt.Rows.Add(123.4567); dt.Rows.Add(123.45678); this.dataGridView1.DataSource = dt; this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting); } void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0 &amp;&amp; e.RowIndex != this.dataGridView1.NewRowIndex) { double d = double.Parse(e.Value.ToString()); e.Value = d.ToString("0.00##"); } } </code></pre> <p><strong>SOURCE:</strong> <a href="http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/95e7e7ef-2e71-412f-abe5-ffbee2c12c18/" rel="nofollow noreferrer">http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/95e7e7ef-2e71-412f-abe5-ffbee2c12c18/</a></p> <p><strong>OUTPUT:</strong></p> <p><img src="https://i.stack.imgur.com/EPAXY.png" alt="Example Output from application"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload