Solved

MYSQL decimal point

Posted on 2008-06-20
5
681 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 265 total points
Comment Utility
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
Comment Utility
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 235 total points
Comment Utility
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 265 total points
Comment Utility
0
 

Author Comment

by:allanch08
Comment Utility
thanks for help everyone
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now