How to remove R $ from NumberFormatter?

3

I'm having the following problem.

Follow my code:

$valores = '530222077.99';
$moeda = new NumberFormatter('pt_BR', NumberFormatter::CURRENCY);
$valores = $moeda->formatCurrency($valores, 'BRL');
echo $valores;

The following code returns me: R$530.222.077,99 . However, I need it to return 530.222.077,99 , that is without the R$ .

    
asked by anonymous 25.02.2016 / 17:53

2 answers

5

When you use NumberFormatter::CURRENCY , you are asking for the "complete package", which includes the currency and thousands and decimal separators.

One option for the numeric part is to use NumberFormatter::DECIMAL

$valores = '530222077.99';
$moeda = new NumberFormatter('pt_BR', NumberFormatter::DECIMAL);
$valores = $moeda->formatCurrency($valores, 'BRL');
echo $valores;

More details can be found at documentation of the class, especially the part of the "predefined constants".

Note that you can create your own formats and masks if you want, using the NumberFormatter::create .

    
25.02.2016 / 18:17
2

Well following the php manual: link

I came to the solution. That in my opinion is much better to use the NumberFormatter because it is a native php library.

The solution for conversion follows.

$valores = '-99999999.99';

$moeda2 = new NumberFormatter('pt_BR', NumberFormatter::CURRENCY);
$moeda2->setTextAttribute(NumberFormatter::NEGATIVE_PREFIX, "-R$");
$moeda2->setTextAttribute(NumberFormatter::NEGATIVE_SUFFIX, "");

$valores = $moeda2->formatCurrency($valores, 'BRL');

Return -R $ 99.999.999.99

Solution for rollback

$moeda2 = new NumberFormatter('pt_BR', NumberFormatter::CURRENCY);
$valor_puro = $moeda1->parseCurrency($valores, $moeda_bd);

Return 99999999.99

    
26.02.2016 / 17:40