Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Displaying Currency Nicely


Go to End


7 Posts   6066 Views

Avatar
mrsteveheyes

Community Member, 15 Posts

31 August 2011 at 10:01pm

Edited: 31/08/2011 10:02pm

Hi there. I am using the currency object as one of the fields in an object on Silverstripe. However; whenever I display it in the temaple it has four digits after the decimal point. I just wondered if there is anyway of stopping it and it only showing two. Below is an example of the code:

<% control Courses %>
	<tr>
		<td class="first"><a href="{$Top.BaseHref}{$Top.URLSegment}/{$URLSegment}">$Title</a></td>
		<td>$Duration</td>
		<td class="highlight">{$PriceCurrency}{$PriceAmount}
		</td>
		<td class="last"><a class="date-link" href="/booking/course/{$ID}">Find Dates</a></td>
	</tr>
<% end_control %>

<tr>
	<td class="first"><a href="{Link Removed}">Fire Safety Course</a></td>
	<td></td>
	<td class="highlight">£0.0000</td>
	<td class="last"><a class="date-link" href="/booking/course/2">Find Dates</a></td>
</tr>

Hope someone can help.

Cheers,
Steve

Avatar
martimiz

Forum Moderator, 1391 Posts

31 August 2011 at 11:54pm

You could use $PriceCurrency.Nice in your template...

Avatar
mrsteveheyes

Community Member, 15 Posts

2 September 2011 at 10:31pm

Thanks martimiz but it didn't work. It didn't show up anything if I did that.

Other options I've tried that didn't work are:

$PriceCurrency.Format
$PriceCurrency.Currency
$PriceCurrency.Decimal

Anyone got any other suggestions?

Cheers,
Steve

Avatar
mrsteveheyes

Community Member, 15 Posts

2 September 2011 at 11:20pm

I figured it out! We we're nearly there with Nice. But it should of been attached to the object, not the string of it. So to get it to work I used:

$Price.Nice

And it worked :)

Cheers,
Steve

Avatar
CHD

Community Member, 219 Posts

5 March 2012 at 9:10am

For anybody having problems getting currency symbols to display with .Nice - use the code for the symbol:
Currency::setCurrencySymbol('&pound;');

Avatar
Haddog

Community Member, 10 Posts

8 January 2015 at 1:04pm

Hi there i have a site and was just wondering how to change the output of the number form have 4 zeros after the decimal place to have just 2

Avatar
darianno

Community Member, 14 Posts

27 December 2016 at 1:22am

I needed format ex.: 1.000,00 € so here's my solution if anyone will need:

CurrencyExtension.php

class CurrencyExtension extends DataExtension {

  public function CustomPriceFormat() {
    $val = number_format(abs($this->owner->value), 2, ',', '.') .' '. $this->owner->config()->currency_symbol;
    if($this->owner->value < 0) return "($val)";
    else return $val;
  }

}

_config.yml

Currency:
  extensions:
    - CurrencyExtension

mysite/_config.php

Currency::setCurrencySymbol('&euro;');

my_template.ss

$Price.CustomPriceFormat

tested in SS 3.5.1