C# 通过 CultureInfo 格式化字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1266093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:26:04  来源:igfitidea点击:

Format string by CultureInfo

c#asp.netcultureinfo

提问by Waheed

I want to show pound sign and the format 0.00 i.e £45.00, £4.10 . I am using the following statement:

我想显示英镑符号和格式 0.00 即 £45.00, £4.10 。我正在使用以下语句:

<td style="text-align:center"><%# Convert.ToString(Convert.ToSingle(Eval("tourOurPrice")) / Convert.ToInt32(Eval("noOfTickets")), new System.Globalization.CultureInfo("en-GB")) %></td>

But it is not working. What is the problem.

但它不起作用。问题是什么。

Can any one help me???

谁能帮我???

采纳答案by adrianbanks

Use the Currencystandard format string along with the string.Formatmethod that takes a format provider:

使用Currency标准格式字符串以及采用格式提供程序的string.Format方法:

string.Format(new System.Globalization.CultureInfo("en-GB"), "{0:C}", amount)

The CultureInfo can act as a format provider and will also get you the correct currency symbol for the culture.

CultureInfo 可以充当格式提供程序,还可以为您提供正确的文化货币符号。

Your example would then read (spaced for readability):

然后您的示例将读取(为了可读性而间隔):

<td style="text-align:center">
    <%# string.Format(new System.Globalization.CultureInfo("en-GB"), 
                      "{0:C}", 
                      Convert.ToSingle(Eval("tourOurPrice")) 
                             / Convert.ToInt32(Eval("noOfTickets")))
    %>
</td>

回答by Dewfy

Try specify exact currency format

尝试指定确切的货币格式

String.Format(...CultureInfo("en-GB"), "{0:C}"....

回答by Patrick McDonald

How about

怎么样

<%# (Convert.ToSingle(Eval("tourOurPrice")) / Convert.ToInt32(Eval("noOfTickets"))).ToString("C", New System.Globalization.CultureInfo("en-GB")) %>

回答by Phaedrus

This should work:

这应该有效:

<td style="text-align:center">
<%# String.Format( new System.Globalization.CultureInfo("en-GB"), "{0:c}", Convert.ToSingle(Eval("tourOurPrice")) / Convert.ToInt32(Eval("noOfTickets")) %>
</td>

回答by Mark

I wanted to add an additional related answer to show how to use a cloned CultureInfo object in a string.Format() or StringBuffer.AppendFormat(). Instead of currency though, my need was to format the AM/PM designator for my employer's style guide. Here is what I did:

我想添加一个额外的相关答案,以展示如何在 string.Format() 或 StringBuffer.AppendFormat() 中使用克隆的 CultureInfo 对象。虽然不是货币,但我需要的是为我雇主的风格指南格式化 AM/PM 指示符。这是我所做的:

var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.AMDesignator = "a.m.";
culture.DateTimeFormat.PMDesignator = "p.m.";
....
var msg = new StringBuilder();
msg.AppendFormat(culture,"Last modified: {0:M/d/yyyy h:mm tt}", ad.DateModified);

You can do the same thing with string.Format():

你可以用 string.Format() 做同样的事情:

string strMsg = string.Format(culture, "Last modified: {0:M/d/yyyy h:mm tt}", ad.DateModified);