MYSQL decimal point

Posted on 2008-06-20
Last Modified: 2013-12-12
Hello experts.

I've come up with a simple 5-star rating system. Everything works fine but the calcualted answer is too long. ie. 2.4444 but I'd like it to be 2.44.

Do I be make this adjustment in MYSQL or PHP and how would i do it?

MYSQL statement:

SELECT ((1star+2star*2+3star*3+4star*4+5star*5)/(1star+2star+3star+4star+5star))
FROM rating
WHERE prod_id = colname

PHP to print answer is attached:

<?php echo $row_rsRating['((1star+2star*2+3star*3+4star*4+5star*5)/(1star+2star+3star+4star+5star))']; ?>

Open in new window

Question by:allanch08
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 48

Accepted Solution

hernst42 earned 265 total points
ID: 21835225
You can do it in PHP with:

echo number_format($row_rsRating['((1star+2star*2+3star*3+4star*4+5star*5)/(1star+2star+3star+4star+5star))'],2);

Author Comment

ID: 21835254
thanks for quick reply, works a treat. as a matter of interest how would I do it in mysql?
LVL 19

Assisted Solution

LordOfPorts earned 235 total points
ID: 21835257
You can give an alias to the SQL value returned in order to simplify it:

SELECT ((1star+2star*2+3star*3+4star*4+5star*5)/(1star+2star+3star+4star+5star)) AS FinalRating
FROM rating
WHERE prod_id = colname

Then in PHP output it in the following manner:

<?php echo sprintf("%1\$.2f", $row_rsRating['FinalRating']); ?>

LVL 48

Assisted Solution

hernst42 earned 265 total points
ID: 21835265

Author Comment

ID: 21835279
thanks for help everyone

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question