简体   繁体   English

CultureInfo格式化字符串

[英]Format string by CultureInfo

I want to show pound sign and the format 0.00 ie £45.00, £4.10 . 我想显示英镑符号和格式0.00即£45.00,£4.10。 I am using the following statement: 我使用以下声明:

<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??? 谁能帮我???

Use the Currency standard format string along with the string.Format method 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>

怎么样

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

尝试指定确切的货币格式

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

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>

I wanted to add an additional related answer to show how to use a cloned CultureInfo object in a string.Format() or StringBuffer.AppendFormat(). 我想添加一个额外的相关答案,以展示如何在string.Format()或StringBuffer.AppendFormat()中使用克隆的CultureInfo对象。 Instead of currency though, my need was to format the AM/PM designator for my employer's style guide. 虽然不是货币,但我需要为我的雇主风格指南格式化AM / PM指示符。 Here is what I did: 这是我做的:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM