8
i write in the database the values of the products in this way:
For example:
R$ 5.000,00
How do I do in PHP to make this number stay 5000? That is to say "shape"?
Do you have a function that fits any number? Something like that?
8
i write in the database the values of the products in this way:
For example:
R$ 5.000,00
How do I do in PHP to make this number stay 5000? That is to say "shape"?
Do you have a function that fits any number? Something like that?
11
I think it’s the NumberFormatter::parseCurrency()
that you want.
$formatter = numfmt_create('pt_BR', NumberFormatter::CURRENCY);
var_dump(numfmt_parse_currency($formatter, "R$ 5.000,00", "BRL"));
I put in the Github for future reference.
I don’t know if this module is available by default on the premises. I tried to run on ideone and it was not.
You can do it manually too, but it takes work and it’s easy to make a mistake, not treat any situation, solve badly formatted numbers, etc.
To have access to these functions/objects it is necessary to enable lntl before.
4
you can do it in a simpler way too, see if it works:
setlocale(LC_MONETARY, 'pt_BR');
echo money_format('%i', $number) . "\n";
Just one observation money_format does not work in windows.
@Did I miss something or does it not work for this question? As far as I know this converts the number to the currency format, and the question is to convert a number that is in currency format and return to a single number.
@bigown he edited the question.
No, he didn’t edit it. He has log edition. http://answall.com/posts/79898/revisions
the money_format
takes a number and foramta, instead of taking a formatted number and taking the format
2
The conversion can be done simply like this:
$str = '99.999,99';
$str = str_replace('.', '', $str); // remove o ponto
echo str_replace(',', '.', $str); // troca a vírgula por ponto
// resulta em 99999.99
You can also use the class numberformatter, specifically the method parsecurrency, if it is enabled in the environment where it wants to execute the code. I prefer to sanitize with the str_replace
, as shown above because normally this class is not installed by default. In a shared hosting, for example, you will hardly be able to have this feature and ultimately have to implement with str_replace
or regex.
Browser other questions tagged php string formatting monetary-value
You are not signed in. Login or sign up in order to post.
number formatting
– rray