Format string by CultureInfo

后端 未结 5 1075
面向向阳花
面向向阳花 2020-12-03 00:30

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

<%# Convert.To         


        
相关标签:
5条回答
  • 2020-12-03 01:13

    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:

    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 strMsg = string.Format(culture, "Last modified: {0:M/d/yyyy h:mm tt}", ad.DateModified);
    
    0 讨论(0)
  • 2020-12-03 01:14

    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>
    
    0 讨论(0)
  • 2020-12-03 01:21

    Use the Currency standard format string along with the string.Format method that takes a format provider:

    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.

    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>
    
    0 讨论(0)
  • 2020-12-03 01:22

    Try specify exact currency format

    String.Format(...CultureInfo("en-GB"), "{0:C}"....
    
    0 讨论(0)
  • 2020-12-03 01:23

    How about

    <%# (Convert.ToSingle(Eval("tourOurPrice")) / Convert.ToInt32(Eval("noOfTickets"))).ToString("C", New System.Globalization.CultureInfo("en-GB")) %>
    
    0 讨论(0)
提交回复
热议问题