?
Solved

MYSQL decimal point

Posted on 2008-06-20
5
Medium Priority
?
705 Views
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

0
Comment
Question by:allanch08
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 1060 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);
0
 

Author Comment

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

Assisted Solution

by:LordOfPorts
LordOfPorts earned 940 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']); ?>
 

0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 1060 total points
ID: 21835265
0
 

Author Comment

by:allanch08
ID: 21835279
thanks for help everyone
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 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