Formatcurrency And Format

Hi,

I’m trying to format a number with no decimals and adding the currency symbol. I get an error if I try to use ‘formatNumber’: Call to protected method CNumberFormatter::formatNumber() from context 'CBaseController.

The closest thing I have is this:




Yii::app()->numberFormatter->formatCurrency($data->total_amount, 'EUR')



I got this: €500.00, and I want this: €500

So I tried with format like this:




Yii::app()->numberFormatter->format('#,##0', $data->total_amount, 'EUR')



but then I don’t get the currency symbol, any suggestion?

Thanks!

Anyone? I have tried several times many options, but I don’t get this work

Check the top documentation of CNumberFormatter: You can use a ‘¤’ sign as placeholder for the currency.

I already tried to use it, like this:




<?php echo Yii::app()->numberFormatter->format('¤#,##0', $data->total_amount, 'EUR');?>



But I get this: �2,500

:(

Then your server probably doesn’t send the right utf-8 charset header. See here: http://www.yiiframework.com/wiki/16/how-to-set-up-unicode#hh6

Hi Mike, thanks for your replies.

I have the charset variable configured in /config/main.php as utf-8 that generates a html like this:


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and also I have the variable default_charset = “iso-8859-1” configured in php.ini, also tried with default_charset = “utf-8”, it’s still not working. Any other thing I could check?

Thanks!

I think there’s some confusion here :)

  1. If you set charset in your config/main.php this will not generate the <meta…> tag in HTML for you. The tag is rather in the default views/layouts/main.php.

  2. If your webserver sends another Content-Type header the tag will not help you. The HTTP header has highest precedence. So you need to check that.

  3. If you want to use utf-8 then why do you set the default_charset to "iso-8859-1" in your php.ini? It should be "utf-8".

Maybe the howto was a bit confusing. I’ve cleaned it up a little so maybe you read that again.

Hi again,

I have been trying to solve this for a while now… I just don’t know what else I can check.

I have set a utf-8 in the http header and also I have configured utf-8 as default charset in php.ini, also I have tried in several ways the CNumberFormatter… :(

I’m not really sure if it’s a charset problem, because with this:




Yii::app()->numberFormatter->formatCurrency($data->total_amount, 'EUR');



I get this: €5,000.00

I mean, the currency symbol is showed properly, if I try this instead:




Yii::app()->numberFormatter->format('¤#,##0', $data->total_amount, 'EUR');



I get this: �5,000

Any other suggestion? I’m stuck here.

Thanks in advance!

What happens if you:

  1. Put a € symbol in a raw test.html file and open that in a browser?

  2. Put “echo ‘€’;” in a test.php file and open that in a browser?

If both don’t work, you still get the wrong charset in your HTTP header. You should verify it e.g. with Firebug or with the Chrome Developer tools (Ctrl + Shift + i -> Network).

Hi Mike, thanks for your help, again.

The first (test.html) works perfectly, it shows the symbol correctly, however the second one (test.php) shows the same weird symbol �, I have this inside head section:


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and also, by checking the page information with Firefox I find that the charset is correctly set to utf-8 (attached images), what else can I do?

4353

charset.png

This is really odd. Your webserver configuration seems right, as it sends the correct Content-Type. And even with PHP’s default settings the simple PHP example should just work. So you probably somehow managed to misconfigure PHP. If you have set default_charset in your php.ini, disable that again. It’s not required and should be left empty. Apart from that i’d even remove that meta tag (at least while you’re testing). It’s not required either and just adds confusion. You can add it back later. It will only be useful if someone saves your webpage locally. For the web, the HTTP Content-Type header is all you need.

EDIT:

Also disable all the mbstring.* settings you might have in your php.ini. BTW What operating system do you use?

Hi Mike,

I have disabled the default_charset and mbstring.* (it was not enabled previously). Also I have removed the meta_tag. I’m getting the same result, I’m really lost with it, if I try this:




Yii::app()->numberFormatter->formatCurrency($data->total_amount, 'EUR');



It shows me the symbol correctly €465.00, but my goal is to have the symbols and without decimals, so with this doesn’t work:




Yii::app()->numberFormatter->format('¤#,##0', $data->total_amount, 'EUR');



it’s really weird.

I’m using windows in my localhost and unix in my online server, it doesn’t work in either of both.

Thanks again for your help, any other suggestion is welcome.

I found the solution for this problem, bad me… I was saving the .php file in ANSI mode instead of UTF8!!!

Now this works:




Yii::app()->numberFormatter->formatCurrency($data->total_amount, 'EUR'); //shows 500.00€

Yii::app()->numberFormatter->format('¤#,##0', $data->total_amount, 'EUR'); //shows €500



The first line put the currency symbol depending on the locale (‘es’, ‘en’, etc) and the second one the currency symbol is located as setted in the pattern, my question now is:

How to show a number without decimals using formatCurrency (so I can take the advantage of i18n)??

Any help is more than welcome, thanks!

This works for me, hope it helps


$format = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$string = $format->formatCurrency($this->price, $this->currency);
echo $string;