Number formatting (PHP)

Asked

Viewed 41,336 times

15

I created a function cotacaoDolar(); who returns at the end:

return str_replace(",",".",$texto_dolar);

And the result, appears here:

echo  $i['sellingStatus'][0]['currentPrice'][0]['__value__'] * cotacaoDolar();

How do I format the value (['__value__']) quoted above?

It’s like this: R$ 271.309389 ou R$ 1119.2445

The right thing would be: R$ 271,30 ou R$ 1.119,24

5 answers

25


Use the function number_format();

$valor = 12345678900;

echo number_format($valor,2,",",".");
// 123.245.678.900,00
  • Beauty, it worked like this: $value = $i['sellingStatus'][0]['currentPrice'][0]['value'] * cotacaoDolar(); echo number_format($value,2,",",".");

13

In PHP 5.3 there is already one class for currency formatting. The first argument of NumberFormatter() is the currency that is based on ISO 4217

$valores = array('530077.99','31459.89', '2899.39', '600.51', '13', '9', '0.25');
$formatter = new NumberFormatter('pt_BR',  NumberFormatter::CURRENCY);
foreach($valores as $item){
    echo  $formatter->formatCurrency($item, 'BRL') . '<br>';
}

The reverse process, convert a currency value to the pure value to write to the bank for example, can be done using the method parseCurrency

$arr=array('R$530.077,99','R$31.459,89','R$2.899,39','R$600,51','R$13,00','R$9,00','R$0,25');

foreach($arr as $item){
    echo  $formatter->parseCurrency($item, $valor_puro) . '<br>';
}

Example

  • +1 for stimulating the use of new practices. Just remembering that this class is not native, it is part of the library intl.

  • @Henriquebarcelos, well remembered, in windows a installation should be done by removing the ; of that line extension=php_intl.dll in php.ini.

  • Interesting this way, I’m a little layy in php, but I’ll try to understand.

9

Use the number_format function. The default function returns in American format, so the need to pass 2 parameters, in this case the "," and ".".

echo 'R$' . number_format($num, 2, ',', '.');
  • In this case, the result was R $ 0,00

5

Use the function number_format:

<?php

$number = 1234.56;

// Notação Inglesa (padrão)
$english_format_number = number_format($number);
// 1,234

// Notação Francesa
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// Notação Inglesa sem separador de milhar
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

0

This function can help:

// Informa as configurações locais que serão usadas na formatação.
// https://www.php.net/manual/en/function.setlocale.php
setlocale(LC_ALL, 'pt_BR');

/**
 * Formata como moeda um valor de acordo com as informações locais.
 * 
 * @param float $val valor que será formatado
 * @return string
 */
function currency(float $val): string
{
    $fmt = new NumberFormatter(setlocale(LC_MONETARY, null), NumberFormatter::CURRENCY);
    $locale = localeconv();
    return $fmt->formatCurrency($val, $locale['int_curr_symbol']);
}

echo currency(1200.99); // Retorna R$ 1.200,99

Browser other questions tagged

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