Skip to main content
Participating Frequently
April 8, 2007
Answered

PHP/MySQL Decimal Numbers

  • April 8, 2007
  • 1 reply
  • 615 views
Hi,

I'm working on a real estate site. Most of the monthly apartment rental rates are round numbers like $950, however a few have odd prices like $1070.76 (hence the need for decimal data types in MySQL).

On the page where all the apartment listings are, what I want to do is remove the decimal point and zeros if the number is round because all the zeros are distracting (and unnecessary). For example:

I have this snippet of code:
if ($r->maxRent == 0) {
$price = 'Asking rent $';
$price .= $r->minRent;
$price .= ' per month';
} else {
$price = 'Asking rent $';
$price .= $r->minRent;
$price .= '-';
$price .= $r->maxRent;
$price .= ' per month';
}
Which displays this:
Asking rent $810.00 - 950.00 per month
Asking rent $1070.76 - 1200.00 per month
Asking rent $1335.00 per month

But I want things to display like this:
Asking rent $810 - 950 per month
Asking rent $1070.76 - 1200 per month
Asking rent $1335 per month

Do I need to do this with regular expressions (voodoo!) or is there another way? I'm currently in a situation where I have to use rather outdated versions of everything (PHP 4.1.2 / MySQL 3.23) if that makes a difference in this case.

I've looked through my books and tried googling & searching the forums for an answer, but I'm unsure of exactly what it is I should be looking for, so I'm not having any luck. Any assistance would be greatly appreicated.

Regards,
Bev
This topic has been closed for replies.
Correct answer Newsgroup_User
bsoliman wrote:
> But I want things to display like this:
> Asking rent $810 - 950 per month
> Asking rent $1070.76 - 1200 per month
> Asking rent $1335 per month

Actually, the .76 strikes me as more confusing than the .00. I would
simply use round() to give a rounded figure for all rents. However, if
you want to do it your way, add this function to your script:

function checkEven($val) {
return preg_match('/\d+\.00/', $val) ? number_format($val, 0) :
number_format($val, 2);
}

Then change your code to this:

if ($r->maxRent == 0) {
$price = 'Asking rent $';
$price .= checkEven($r->minRent);
$price .= ' per month';
} else {
$price = 'Asking rent $';
$price .= checkEven($r->minRent);
$price .= '-';
$price .= checkEven($r->maxRent);
$price .= ' per month';
}

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

1 reply

Newsgroup_UserCorrect answer
Inspiring
April 8, 2007
bsoliman wrote:
> But I want things to display like this:
> Asking rent $810 - 950 per month
> Asking rent $1070.76 - 1200 per month
> Asking rent $1335 per month

Actually, the .76 strikes me as more confusing than the .00. I would
simply use round() to give a rounded figure for all rents. However, if
you want to do it your way, add this function to your script:

function checkEven($val) {
return preg_match('/\d+\.00/', $val) ? number_format($val, 0) :
number_format($val, 2);
}

Then change your code to this:

if ($r->maxRent == 0) {
$price = 'Asking rent $';
$price .= checkEven($r->minRent);
$price .= ' per month';
} else {
$price = 'Asking rent $';
$price .= checkEven($r->minRent);
$price .= '-';
$price .= checkEven($r->maxRent);
$price .= ' per month';
}

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
bsolimanAuthor
Participating Frequently
April 8, 2007
Thanks so much, David. Only a few minutes before you posted the answer I'd dragged out my regular expressions book with much trepidation—I'm sure you just saved me hours of head-banging. :-)

If the decision were mine, I would also use all round numbers.

Best Regards,
Bev