Is there a way to get a fromatted decimal value with comma as seperator for the decimal place?


#1

4.2

Is there a way to get a fromatted decimal value with comma as seperator?


#2

Could use PHP: number_format - Manual for that?


#3

Yeah I did that way now

the function is part of an extension for my pagetypes:

class MyExtension extends Extension {
		private static $allowed_actions = [
                       'formattedNumber'
             ]
            public function formattedNumber($val){
		    return number_format($val, 2, ',', '.');
	    }
 }

in template I can now use it like that:
$formattedNumber($Number)


#4

That’s a fine solution, but you might want to consider adding an extension to DBFloat instead (if that’s the type of object $Number is, or DBFloat is an ancestor). Then your custom formatting would be available everywhere, not just on page controllers.

public function EuroNice()
{
    return number_format($this->value, $val, 2, ',', '.');
}

I’m actually quite surprised that SilverStripe doesn’t format numbers the way you want automatically based on the current locale.

edit: p.s. you shouldn’t need that $allowed_actions entry unless you’re wanting people to be able to access yoursite.com/your-page-url/formattedNumber