Some fields in my datagrid are bound to numeric fields in the SqlServer database. The default behavior is that the datagrid always displays numbers to the same precision as the database field. But I get trailing zeros. How can I format the datagrid just
to display the significant digits?
Thanks for the reply. Your solution fixes the decimal places. That's not what I want to do. For example I have a field which can have 8 decimal places. Usually though it has 3 or 4 decimal places. So I want to strip off any trailing zeros and just display
all the significant digits. But when there are 8 significant digits, I want to display them all.
...then I created a public method in my page's class file named GetNumberDisplay that takes an object, and returns a string.
Now this function can be defined in two ways. If you know that your numbers (based on the local region) are always be formated like 123.456 (with a dot as the decimal place holder) you can simply write it as...
public static string GetNumberDisplay(object value)
{
string number = value.ToString();
number = number.TrimEnd("0".ToCharArray());
number = number.TrimEnd(".".ToCharArray());
return number;
}
...but if you can't assume this (like there is a chance the ToString method will use a comma as the decimal place holder, like in some cultures), then I have done it like this (in this example, the precision of the number in the database was 3)...
public static string GetNumberDisplay(object value)
{
decimal number = SqlConvert.ToDecimal(value);
string numberDisplay;
if (number == decimal.Truncate(number)) // no decimal places, is a whole number
{
numberDisplay = number.ToString("N0");
}
else if (number * 10 == decimal.Truncate(number * 10)) // dealing with a one decimal place value
{
numberDisplay = number.ToString("N1");
}
else if (number * 100 == decimal.Truncate(number * 100)) // two decimal place value
{
numberDisplay = number.ToString("N2");
}
else
{
numberDisplay = number.ToString(); // this is the 3 decimal place representation
}
return numberDisplay;
}
I just tried your template column and it works fine. I've never used one of these before. It will be usefull for other wierd formatting scenarios. Thanks everyone for your help.
Hoof Hearted
Participant
846 Points
190 Posts
Remove trailing zeros in numeric fields.
May 25, 2007 11:24 AM|LINK
Some fields in my datagrid are bound to numeric fields in the SqlServer database. The default behavior is that the datagrid always displays numbers to the same precision as the database field. But I get trailing zeros. How can I format the datagrid just to display the significant digits?
edurdias
Member
180 Points
45 Posts
Re: Remove trailing zeros in numeric fields.
May 25, 2007 01:22 PM|LINK
Hi, in BoundField, add this property:
DataFormatString = "{0:F2}"
You can see more in : http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx
Eduardo Dias
http://weblogs.asp.net/eduardodias
kaushalparik...
All-Star
26580 Points
3693 Posts
MVP
Re: Remove trailing zeros in numeric fields.
May 25, 2007 01:32 PM|LINK
in gridview; goto edit columns and select the numeric field ; set the below property
DataFormatString={0:F2} (numeric value with fixed format, two decimal ciphers)
other options for formatting are
About data format string here you are an small table with the valid formatting values:
If we execute the application now, we will see the price column nice formatted
hope this helps./.
[KaushaL] || BloG || Twitter
Don't forget to click "Mark as Answer" on the post that helped you.
Hoof Hearted
Participant
846 Points
190 Posts
Re: Remove trailing zeros in numeric fields.
May 25, 2007 01:34 PM|LINK
Thanks for the reply. Your solution fixes the decimal places. That's not what I want to do. For example I have a field which can have 8 decimal places. Usually though it has 3 or 4 decimal places. So I want to strip off any trailing zeros and just display all the significant digits. But when there are 8 significant digits, I want to display them all.
ggorczow
Member
170 Points
26 Posts
Re: Remove trailing zeros in numeric fields.
May 25, 2007 01:53 PM|LINK
This is the way I tackled this one...
I started out by making the column a templated column. Then I made the template look like this...
<ItemTemplate>
<%# GetNumberDisplay(Container.DataItem["someField"]) #>
</ItemTemplate>
...then I created a public method in my page's class file named GetNumberDisplay that takes an object, and returns a string.
Now this function can be defined in two ways. If you know that your numbers (based on the local region) are always be formated like 123.456 (with a dot as the decimal place holder) you can simply write it as...
public static string GetNumberDisplay(object value) { string number = value.ToString(); number = number.TrimEnd("0".ToCharArray()); number = number.TrimEnd(".".ToCharArray()); return number; }...but if you can't assume this (like there is a chance the ToString method will use a comma as the decimal place holder, like in some cultures), then I have done it like this (in this example, the precision of the number in the database was 3)...
Hoof Hearted
Participant
846 Points
190 Posts
Re: Remove trailing zeros in numeric fields.
May 25, 2007 02:24 PM|LINK
Greg,
I just tried your template column and it works fine. I've never used one of these before. It will be usefull for other wierd formatting scenarios. Thanks everyone for your help.
Ian