This is what I am using: <?php $price = $ad['price']; echo number_format($price, 2, '.', ','); ?> Code (markup): Prices are displayed like this: 1,500,000.00 or 1,125.00 or 1.00 I want them to show like this: 1,500,000 or 1,125 or 1 However I still want to show decimals in prices that are 1.09 or 110.75 or 1,111.99 or whatever. I just want to get rid of the decimal zeros. Thanks!
You can use floatval(). However, that will remove all trailing zeros. Another solution would be preg_replace('/\.00/', '', $num).
Ah, ok. I did this number and it seems to be working: <?php $price = $ad['price']; $price = number_format($price, 2, '.', ','); $price = preg_replace('/\.00/', '', $price); echo $price; ?> Code (markup): Probably not the best way. Thanks for the help.
I set your answer as the best answer for pointing me in the right direction. I am sure there are other ways of fixing this issue but as of now I am happy with what I've got. Thanks!
Regular expressions are useful for complicated things, but for this, a simple str_replace() would have sufficed (and would also have been faster).
I think that could probably be condensed, to something like this: <?php echo str_replace('.00','',number_format($ad['price'],2,'.',',')); ?> PHP: Unless you need to use the $price-variable again somewhere else, no need to create another variable to hold an array-value.
you could use round() you can specify how many decimal points... http://php.net/manual/en/function.round.php