Format string by CultureInfo
Asked Answered
S

5

47

I want to show pound sign and the format 0.00 i.e £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???

Sportscast answered 12/8, 2009 at 13:14 Comment(0)
C
108

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>
Cryptology answered 12/8, 2009 at 13:30 Comment(3)
Does this actually work? Your are applying a culture to a string, which I don't think will have any affect.Forestall
This is what I tried: string foo = String.Format(new System.Globalization.CultureInfo("en-GB"), "{0:C}", Convert.ToString(3.01));Forestall
@Joe: ah, I see what you're referring to now. I've updated my answer to correct it.Cryptology
D
3

How about

<%# (Convert.ToSingle(Eval("tourOurPrice")) / Convert.ToInt32(Eval("noOfTickets"))).ToString("C", New System.Globalization.CultureInfo("en-GB")) %>
Downstream answered 12/8, 2009 at 13:21 Comment(0)
B
1

Try specify exact currency format

String.Format(...CultureInfo("en-GB"), "{0:C}"....
Botswana answered 12/8, 2009 at 13:20 Comment(0)
T
1

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>
Thematic answered 12/8, 2009 at 13:27 Comment(0)
O
1

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);
Opener answered 27/9, 2013 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.