How to advance two decimal places in php or javascript

Asked

Viewed 41 times

-5

I’d like to do the following I have the value 1000000 and I have to format it like this 10,000.00 Because the value comes to me already formatted and I can’t use between (mysql) to fetch a value so I do :

$preco = $row->preco;
$newpreco = preg_replace("/[^0-9]/", "",$preco);

Thus obtaining the value without letters and without punctuation this way I can filter using

SELECT * FROM produtos WHERE preco BETWEEN '2000000' AND '5000000';

Obs: 2000000 refers to 20,000.00

however when return this value needed to format it with the scores again.

1 answer

-2


I suggest you use the number_format, see the documentation here

$preco = number_format ($row->preco, 2, ',' ,'.');

Here, if you pass 10,000, it returns 10,000.00. I in your place would do two auxiliary functions, one that already does what you do and the other with the code above for return

  • 1

    Thanks I managed to do this, using the received value divided by 100 and I used number_format the way you showed it worked thanks.

  • Good friend, good friend

Browser other questions tagged

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