Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below code, I am trying to format the value of the cell inside a datagridview in windows form to have thousand separator
But the problem is that it places .00 at the end of the numbers
i.e.
it converts 123456789 to 123,456,789.00

How can I solve this please?
Thanks
C#
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex > 0)
            {
                if (dgv.Columns[e.ColumnIndex].Name.ToLower().Contains("summary"))
                {
                    if (e.Value.ToString() != "")
                    {
                        double d = double.Parse(e.Value.ToString());
                        e.Value = d.ToString("n");
                    }
                }
            }
        }
Posted

Hi,

While formatting the string to define the decimal places, the syntax is as below.


yourdoublestring.ToString("N" + nDecimal)


where nDecimal is the number of decimal places needed.

Refer the below link for more formats.

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^]
 
Share this answer
 
v2
Comments
arkiboys 1-Feb-12 6:31am    
Thank you
manognya kota 1-Feb-12 7:19am    
welcome:)
Try e.Value = d.ToString("N0");.
Refer to Formatting Numeric Results Table (C# Reference)[^].
 
Share this answer
 
v2
Comments
arkiboys 1-Feb-12 6:17am    
Hi,
How can th enumber be formatted to 3 decimal places?
Thanks
Abhinav S 1-Feb-12 6:32am    
Three would be N3.
arkiboys 1-Feb-12 6:31am    
Thank you
Abhinav S 1-Feb-12 6:33am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900