区域设置 IIS6 和 ASP.Net 2 - {0:c} 在 gridview 中返回太多小数

发布于 2024-07-14 10:21:07 字数 1062 浏览 6 评论 0原文

我目前正在将我的网站从现有的网络服务器迁移到新机器。

新机器是WIN Sever 2003,运行IIS6。 该网站使用 ASP.Net 2。

我将以下内容添加到 machine.config 文件中的 部分,以使网站和任何未来的网站改用南非区域设置美国默认设置。

<globalization culture="en-ZA" uiCulture="en-ZA"/>

这基本上有效 - 货币符号已从 $ 更改为 R,因为它们应该,但我在 gridview 中有一个列(绑定字段 - 数据库类型是货币),设置如下:

DataFormatString =“{0:c” }"

[编辑] - 已使用 {0:C} 以及下面 Richard 的建议进行了尝试:仍然没有运气

这仍然返回 10000.0000,而不是 R 10,000.00,因为它在我的旧服务器上。

关于如何解决这个问题有什么想法吗?

谢谢!

[编辑] - 我开始认为这与实际的文化设置关系不大。

我有另一种工作正常的表单:

<ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Balance", "{0:c}") %>'></asp:Label>
</ItemTemplate>

不起作用的表单使用没有模板的 gridview:

<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" DataFormatString="{0:C}" >
    <ItemStyle CssClass="al-r" />
</asp:BoundField>

I am currently moving my website from an existing web server to a new machine.

The new machine is WIN Sever 2003 running IIS6. The site is using ASP.Net 2.

I added the following to the <system.web> section in my machine.config file to get the website and any future sites to use South African regional settings instead of the default US settings.

<globalization culture="en-ZA" uiCulture="en-ZA"/>

This has mostly worked - the currency symbols have changed from $ to R as they should, but I have a column in a gridview (a bound field - the DB type is money) which is set as follows:

DataFormatString="{0:c}"

[Edit] - have tried this with {0:C} as well as per Richard's suggestion below: still no luck

This is still returning 10000.0000 instead of R 10,000.00 as it was on my old server.

Any ideas as to how to fix this?

Thanks!

[Edit] - I'm beginning to think that this has little to do with the actual culture settings.

I have another form where it works fine:

<ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Balance", "{0:c}") %>'></asp:Label>
</ItemTemplate>

The form that doesn't work uses the gridview without a template:

<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" DataFormatString="{0:C}" >
    <ItemStyle CssClass="al-r" />
</asp:BoundField>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-07-21 10:21:07

在您的绑定字段上尝试设置 HtmlEncode="false"。

<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" DataFormatString="{0:C}" HtmlEncode="false">
    <ItemStyle CssClass="al-r" />
</asp:BoundField>

On your bound field try setting HtmlEncode="false".

<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" DataFormatString="{0:C}" HtmlEncode="false">
    <ItemStyle CssClass="al-r" />
</asp:BoundField>
吃素的狼 2024-07-21 10:21:07

MSDN 显示货币为大写 C,而不是小写。

(格式说明符区分大小写。)


更新:现在在此处进行测试。

代码:

class Program {
    static void Main(string[] args) {
        decimal val = 1234567.89M;

        using (var file = new FileStream(args[0], FileMode.Create, FileAccess.Write))
        using (var output = new StreamWriter(file, Encoding.UTF8)) {
            output.WriteLine("Thread culture: " + Thread.CurrentThread.CurrentCulture.Name);
            output.WriteLine("Thread UI culture: " + Thread.CurrentThread.CurrentUICulture.Name);

            var cultures = new[] { "en-US", "en-GB", "af-ZA", "fr-FR", "fr-CA" };
            foreach (var culture in cultures) {
                var ci = new CultureInfo(culture);
                output.WriteLine(String.Format(ci, "{0,-10}: {1:C}", ci.Name, val));
            }
        }
    }
}

给出输出:

Thread culture: en-GB
Thread UI culture: en-US
en-US     : $1,234,567.89
en-GB     : £1,234,567.89
af-ZA     : R 1,234,567.89
fr-FR     : 1 234 567,89 €
fr-CA     : 1 234 567,89 $

对我来说看起来不错。

你如何设定文化?

MSDN shows a capital C for currency, not lower case.

(Formatting specifiers are case sensitive.)


Update: Now test things here.

Code:

class Program {
    static void Main(string[] args) {
        decimal val = 1234567.89M;

        using (var file = new FileStream(args[0], FileMode.Create, FileAccess.Write))
        using (var output = new StreamWriter(file, Encoding.UTF8)) {
            output.WriteLine("Thread culture: " + Thread.CurrentThread.CurrentCulture.Name);
            output.WriteLine("Thread UI culture: " + Thread.CurrentThread.CurrentUICulture.Name);

            var cultures = new[] { "en-US", "en-GB", "af-ZA", "fr-FR", "fr-CA" };
            foreach (var culture in cultures) {
                var ci = new CultureInfo(culture);
                output.WriteLine(String.Format(ci, "{0,-10}: {1:C}", ci.Name, val));
            }
        }
    }
}

Gives output:

Thread culture: en-GB
Thread UI culture: en-US
en-US     : $1,234,567.89
en-GB     : £1,234,567.89
af-ZA     : R 1,234,567.89
fr-FR     : 1 234 567,89 €
fr-CA     : 1 234 567,89 $

Which looks OK to me.

How are you setting the culture?

木格 2024-07-21 10:21:07

感谢您对此的帮助。

我通过安装 ASP.Net 2.0 的 Service pack 2 解决了这个问题 - 重新启动后,网格在 {0:C} 下工作正常。

这个问题在生产环境中会自行修复,因为它将被设置为自动更新。 目前,我们在本地办公网络上拥有新服务器,而我们的防火墙似乎限制了更新。

经验教训:始终确保所有服务器软件都是最新的!

thanks for your help with this.

I solved this problem by installing Service pack 2 of ASP.Net 2.0 - after the reboot, the grid worked fine with {0:C}.

This problem would have fixed itself in the production environment as it will be set to update automatically. Currently we have the new server on the local office network and our firewall seems to be restricting the updates.

Lesson learned: always make sure that all your server software is up to date!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文