Error formatting number in php

Asked

Viewed 116 times

1

I have this value: 47830,60

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

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

but this error returns to me:

A non well Formed Numeric value encountered

Note: I tried a cast float but it’s zero pennies.

  • 2

    Change 47830,60 for 47830.60 and see if it solves.

  • 1

    Thanks!. Solved and used the str_replace to do this

  • You can take advantage and create an answer to your own question, explaining how you solved it and putting the code. So it ends up being a reference for future readers with the same problem.

1 answer

1


Solution:

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

Explanation:

Since my initial value has a comma to separate the pennies, I needed to use the str_replace to exchange the comma for a point and leave the value like this: 47830.60. With this it was possible to do the number_formar hassle-free.

Browser other questions tagged

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