How to remove R$ from Numberformatter?

Asked

Viewed 268 times

3

Guys, I have the following problem.

Follows my code:

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

The following code returns to me: R$530.222.077,99. But I need you to call me back 530.222.077,99, that is to say without the R$.

2 answers

5


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

An 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 seen on documentation of the class, in particular the "predefined constants".

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

  • I’m having another problem. if I put a 2500 value it returns me 2,500 to the indez of 2,500.00. You know how to solve this?

  • 2

    Testing $moeda->setAttribute( NumberFormatter::MIN_FRACTION_DIGITS, 2 );

  • 1

    perfect worked 100%

  • Could you give me one more help? I’m using the CURRENCY form to display totals in report, because it already comes with the R$. The problem is that when you format a negative number it looks like this: (R$4,444.44) Since I need it to look like this: R$-4,444.44 You know how to solve this?

  • R$ -4.444,44 does not exist :) What you can do, is use an IF and put, for example, parentheses around (or a minus sign before the R$). As I commented on another occasion, if you really want to customize the output a lot, it is better to have a proper formatting function.

  • In case how do I get like this -R$4,444.44? Because there has to be a way for me to represent correct negative value?

Show 1 more comment

2

Good following the php manual: http://php.net/manual/en/numberformatter.formatcurrency.php

I arrived at the solution. Which in my opinion is much better to use Numberformatter because it is a native php library.

Follow the solution for conversion.

$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');

Returns -R$99,999,999

Solution for reversal

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

Returns 99999999.99

Browser other questions tagged

You are not signed in. Login or sign up in order to post.