tags:

views:

613

answers:

4

i am trying to remove the .00 at the end of the price

 function getMoney($mon) {
  setlocale(LC_MONETARY, 'en_US');
  return money_format('%.2n', $mon);
 }

Thank You,

A: 

It seems on first inspection, your code is asking for the cents to be displayed by means of the '%.2n', but I notice the following link on the PHP function reference which includes a warning about set_locale() on Debian.

You're not using Debian, are you?

pavium
+2  A: 

Try using this version of the money_format() function:

money_format('%.0n', $mon);

According to the PHP documentation on money_format (http://us.php.net/manual/en/function.money-format.php), making the format string '%.2n' will force the number to be displayed with the decimal point and 2 decimal places to the right of the decimal point, no matter what the cents are, and then format the number according the the locale's national currency format.

By replacing that with '%.0n', you will always remove the decimal at the end of the money. Of course, this will remove the decimal for all values, not just those with .00 at the end. If you want to conditionally remove the .00, check to see if the value of $mon has a fractional part, and if so, display it with '%0.2n'.

Ben Torell
A: 

Please check the PHP manual:

You need to set the right precision as such:

function getMoney($mon) {
  setlocale(LC_MONETARY, 'en_US');
  return money_format('%.0n', $mon);
 }
Jefe
A: 

So you want to remove the cents, but only if there's no cents? There's always the quick and dirty regex:

preg_replace("/\\.00^/", "", money_format('%.2n', $mon))
nickf