Error formatting number in php [duplicate]

1

I have this value: 47830,60

I want to leave 47.830,60 . For this I used the function number_format like this:

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

But I get this error:

  

A well-formed numeric value encountered

Note: I've tried a cast float but it's zeroing the cents.

    
asked by anonymous 13.06.2018 / 19:37

1 answer

1

Solution:

<?php echo "R$ ".number_format(str_replace(',', '.', $valor),2,',','.'); ?>

Explanation:

As my initial value has a comma to separate the cents, I needed to use str_replace to change the comma by a period and leave the value as: 47830.60 . With this it was possible to do number_formar without problems.

    
13.06.2018 / 22:37