?
Solved

MYSQL decimal point

Posted on 2008-06-20
5
Medium Priority
?
696 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
[X]
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
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

741 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