String format, thousand separator and decimals

H

hansiman

In a datagrid I want to format a sql db bigint field with zero
decimals as 1.000.000 where full stop (.) separates thousands.

I use:
<%# DataBinder.Eval( Container, "DataItem.Amount", "{0:0,0}") %>

....and get 1,000,000 (where there is a value and 00 (double zero)
where there is none.

The comma I know is thousand separator, however, I don't know how to
change it to a full stop (.).

That I get 00 (double zero) I don't understand...

Please give me a hint.

M
 
G

Guest

Hi,

You need to change the NumberGroupSeparator so it doesn't use "," but uses
".". That's in the System.Globalization namespace. You'll probably need a
helper function to do the work. See the example code below.

I've used a Long in the sample because it seems to be the same as a SQL
bigint.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<%@ Page Language="VB" %>
<script runat="server">

sub Page_Load
if not ispostback then
datagrid1.datasource=CreateDataSource()
datagrid1.databind
end if

end sub
Function Fixbigint(lng as long) as string
Dim nfi As System.Globalization.NumberFormatInfo = _
New System.Globalization.CultureInfo _
("en-US", False).NumberFormat
' lng=9223372036854775807
nfi.NumberGroupSeparator ="."
return lng.ToString("N", nfi)
end function

Function CreateDataSource() As system.data.DataTable
Dim dt As New system.data.DataTable
Dim dr As system.data.DataRow
dt.Columns.Add(New system.data.DataColumn _
("id", GetType(Int32)))
dt.Columns.Add(New system.data.DataColumn _
("itemdesc", GetType(String)))
dt.Columns.Add(New system.data.DataColumn _
("along", GetType(Long)))
dt.Columns.Add(New system.data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 92233720368547758 + i
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
<p>
</p>
<p>
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="along">
<ItemTemplate>
<asp:Label runat="server" text='<%#
Fixbigint(DataBinder.Eval(Container, "DataItem.along")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</p>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top