Convert decimal value to currency value

Asked

Viewed 153 times

0

I need to convert the number 4.500.00 for 4500,00.

I’m doing it like this but it’s not working:

$valor3=number_format($valor, 3, ',', '.');

What I could do?

  • 2

    The function number_format wait as first parameter one float. If its value is 4.500.00, he sure ain’t a float. Start by checking this out.

  • would have some reference?

  • 2
  • 4

    Possible duplicate of Format string/float for PHP currency

2 answers

1

Friend, in your question I think you meant:

I need to convert this number 4,500.00 for this one 4500,00. I’m doing it like this but it’s not working.

If that’s right, I took a test like this and it worked:

<?php
    $valor  = 4500;
    $valor3 = number_format($valor, 2, ',', '');
    echo $valor3;    
?>
  • put value=4.500.00 is not working

  • Wagner, I don’t understand your doubt. To use the number_format function you need a Float, the way you are talking, would be a String "4,500.00". If you can describe your need better it would be easier to help you...

1


In this example, the pennies must always exist, otherwise the output will be wrong.

$num = "1.154.500.00";
$valor = substr_replace($num, ',', -2, -2);
$valor = str_replace('.', '', $valor);
echo "R$ " . $valor . "<br>";

Exit: R$ 1154500,00

Browser other questions tagged

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