Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert a decimal value to a currency string for display in HTML

I wanting to show prices for my products in my online store. I'm currently doing:

<span class="ourprice">
     <%=GetPrice().ToString("C")%>
</span>

Where GetPrice() returns a decimal. So this currently returns a value e.g. "£12.00"

I think the correct HTML for an output of "£12.00" is "&pound;12.00", so although this is rendering fine in most browsers, some browsers (Mozilla) show this as $12.00.

(The server is in the UK, with localisation is set appropriately in web.config).

Is the below an improvement, or is there a better way?

<span class="ourprice">
     <%=GetPrice().ToString("C").Replace("£","&pound;")%>
</span>
like image 360
AndyM Avatar asked Feb 04 '26 21:02

AndyM


1 Answers

Try this, it'll use your locale set for the application:

<%=String.Format("{0:C}",GetPrice())%>
like image 102
Nick Craver Avatar answered Feb 06 '26 11:02

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!