Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal number comma styling

Tags:

c#

asp.net

I have some "double" values in a gridview on an aspx page. I need to use comma-styling in these values like:

original value = 1986.05 required value = 19,86.05

original value = 51986.05 required value = 5,19,86.05

I am using the following code for this:

e.Row.Cells[6].Text = String.Format("{0:##,##,##.##}", Convert.ToDouble(e.Row.Cells[6].Text));

It is showing the value as 1,986.05 but I need it as 19,86.05

Please help!

like image 266
DeepakVerma Avatar asked Jul 14 '26 06:07

DeepakVerma


1 Answers

You can use a custom CultureInfo with 2-digit number grouping to achieve this:

CultureInfo culture = new CultureInfo(string.Empty, true)
{
    NumberFormat = { NumberGroupSizes = new int[] { 2 } }
};
Console.WriteLine((1986.05).ToString("N", culture));

Output:

19,86.05

The documentation set for NumberGroupSizes is available here that describes this.

In your code you could implement it like:

CultureInfo culture = new CultureInfo(string.Empty, true)
{
    NumberFormat = { NumberGroupSizes = new int[] { 2 } }
};
e.Row.Cells[6].Text = Convert.ToDouble(e.Row.Cells[6].Text).ToString("N", culture);
like image 93
Martin Avatar answered Jul 15 '26 21:07

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!