suttercain Posted February 27, 2007 Share Posted February 27, 2007 Good morning everyone, I have a question. I know it's pretty basic but I was unable to find the answer on my own on php.net so maybe someone can help me out. I am echoing a varchar column from a MySQL database. In this column I have some integers and some doubles. Exp: 1.5, 2, 3.0, 5. WHAT I NEED: The numbers that already have a decimal are echoed in the correct format. I need the numbers with no decimal (2, 5, etc.) to be echoed with one decimal to match the others in the column. Exp: I need 2 to be echoed as 2.0, 5 as 5.0, etc. Here is the code that is being echoed: echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_class]</td>\n"; echo "<td>$row[disp] L</td>\n"; echo "<td>$row[fuel]</td>\n"; <--------------- THIS IS WHERE I NEED THE DECIMAL POINT echo "</tr>\n"; Anyone have any suggestions? Thank you in advance. Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted February 27, 2007 Share Posted February 27, 2007 echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_class]</td>\n"; echo "<td>$row[disp] L</td>\n"; echo "<td>".sprintf( '%01.1f',$row[fuel])."</td>\n"; <--------------- THIS IS WHERE I NEED THE DECIMAL POINT echo "</tr>\n"; Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 27, 2007 Author Share Posted February 27, 2007 I copied and pasted the above code. It turned all the echoed numbers to 0.0 Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 look into number_format(). echo number_format($var, 1); Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted February 27, 2007 Share Posted February 27, 2007 boo_lolly's code will work. However, I tested my original code again and there isn't really a reason it should return 0. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.