Truncating Decimals in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stock-Wars
Forum Newbie
Posts: 3
Joined: Mon Oct 27, 2003 8:19 pm

Truncating Decimals in PHP

Post by Stock-Wars »

All right, I have code that pulls out of the MySQL database, and it returns a decimal with four decimal places. The problem is when the values are displayed in the php I would like them to have only 2 decimal places, and just truncate the other 2, because anytime all four decimal places are needed they are pulled directly from the DB and not shown on the page. Any help as far as functions, or links to needed functions would be greatly appreciated, thank you.


Here is how the Code goes:

Code: Select all

while($myrow = mysql_fetch_array($sidrows))
{
  $dbsid = $myrow["sid"];
  $dbsymbol = $myrow["symbol"];
  $dbsname = $myrow["sname"];
  $dbcurr = $myrow["current_price"];
  $dbstart = $myrow["start_price"];
  
  echo "<tr><td>$dbsymbol</td> ";
  echo "<td><a href='stockdesc.php?stocksymbol=$dbsymbol'>$dbsname</a></td>";
  
  if($dbcurr > $dbstart)
  {
    echo "<td><font color="#00FF00">$dbcurr</font></td>";
  }
  else if($dbcurr < $dbstart)
  {
    echo "<td><font color="#FF0000">$dbcurr</font></td>";
  }
  else if($dbcurr == $dbstart)
  {
    echo "<td>$dbcurr</td>";
  }
// display more information, not relevant
$dbcurr is the current stock price in the database, which is the number with 4 decimal places, but since its money, and money is normally measured in 2 decimal places, I would like it to be shown in only 2 decimal places with the other two truncated. Once again, thanks for any help you may offer.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Round() exists in both PHP and mysql. Where you use it, is up to you.

http://se.php.net/manual/en/function.round.php
http://www.mysql.com/doc/en/Mathematical_functions.html

Code: Select all

<?php
    $array[] = 4.44344;
    $array[] = '4.44344';
    $array[] = 5.5;
    $array[] = 1224.44235;
    foreach ($array as $value) {
        echo round($value,2).'<br />';
    }
?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

http://us2.php.net/manual/en/language.types.float.php

dunno what to say from there, but that'd be the place i start
Stock-Wars
Forum Newbie
Posts: 3
Joined: Mon Oct 27, 2003 8:19 pm

Post by Stock-Wars »

Thank you very much, now I know why there are so many members here, thats the quickest answer i've ever gotten, and certainly the most helpful, thank you :)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

not just speed here. for the most part the quality is great too.

i found most other place severly lack in both.

the only one that can compare in quality is actually better at client side and some tf the users make it be a question of who's respondin g at each place on db questions. but isn't as fast. lately it's been having an issue since it's moving to vb3 from vb2. i normally mirror my posts so i have a thread on anything at each place. the db stuff is always answered faster there by the owner, who is one of the best people i've come across for explaining db things.

http://www.webmaster-forums.net/

the only way either site could really get better is if they merged (keeping the phpbb forums since i like this feel better than vb2 and vb3)
Stock-Wars
Forum Newbie
Posts: 3
Joined: Mon Oct 27, 2003 8:19 pm

Post by Stock-Wars »

Yes, I agree, reading through some of the forums this is a really great site, comparable to only one other site I have ever been too, which is less internet related as programming related. And if those are the webdesign forums that I am a member of, I had a run in with the admin (arien talabac I believe his name was) before it changed to web-design centered forums :D
Post Reply