Swap a dot for a comma and add a dot

Asked

Viewed 1,166 times

2

How do I add the comma to the decimal place and the dot to the thousands.

Example:

$pagamento = R$ 1000.50;

/* trecho a fazer a operação */

echo $pagamento; //saída: "R$ 1.000,50"

2 answers

7


Use the function number_format for its amendment

$valor = 1000.50;
$valor = number_format($valor, 2 , ",", ".");

2

Dude

$moeda = 'R$ 1.550,52';

$valor = preg_replace('/[^0-9]/', '', $moeda);    
$valor = bcdiv($valor, 100, 2);
$valor = strtr($valor, '.', ',');

echo $valor;

Or if you want to be separated by .

$moeda = 'R$ 1.550,52';

$valor = preg_replace('/[^0-9]/', '', $moeda);    
$valor = bcdiv($valor, 100, 2);
$valor = strtr($valor, ',', '.');

echo $valor;
  • "Comma and period"

  • 1

    real, I set here to fit better what the young man needed.

  • I opted for the simplest not to increase the lines of my code in my table

  • The point is that if you guarantee that the value of $moeda will always be numeric, beauty! If not, this code above will help you!

Browser other questions tagged

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