devilsvein Posted January 25, 2013 Share Posted January 25, 2013 (edited) Basically I've got a table in my database where I have a row called 'm' and its a decimal with 32 character length. when i call it out the data it shows the correct result on screen and even after i've applied the abs function it still works. However when number format comes in to play......it only shows the first digit! Regardless of the decimal places I out in. Yet it works for other results I have from my table. There displayed correctly withs abs and number format. By debugging what I have found is that any number before the colon is displayed. So if we had 5,500 in the table => 5 would be displayed to screen. If 55,000 was in the database, only 55 would show $m = number_format(abs($row['Money']), 2); Edited January 25, 2013 by devilsvein Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/ Share on other sites More sharing options...
Jessica Posted January 25, 2013 Share Posted January 25, 2013 (edited) That's a comma not a colon. And yes, that is how PHP works. http://us2.php.net/manual/en/function.floatval.php You'll want to remove the comma. But I don't think you can even store a comma in a decimal type in MySQL. So... Edit: I just noticed you're in London. PHP uses the American style of . for decimal places and , for thousands separator. I think you can change your local to fix it. Edited January 25, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408235 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2013 Share Posted January 25, 2013 At some point your code is treating $m like it is a number, instead of a formatted string. What's your code from the point where you are assigning the formatted string to $m through to the point where you are displaying it? Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408236 Share on other sites More sharing options...
devilsvein Posted January 25, 2013 Author Share Posted January 25, 2013 (edited) Thanks for the answer. It didn't help BUT it did lead me to find the correct solution. I was using something like (32,0) when it should have been (32,4) in the database size and $m is a number m is for money! Edited January 25, 2013 by devilsvein Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408237 Share on other sites More sharing options...
salathe Posted January 25, 2013 Share Posted January 25, 2013 Edit: I just noticed you're in London. PHP uses the American style of . for decimal places and , for thousands separator. I think you can change your local to fix it. The folks of Londonshire also use those particular characters. The number_format() function is not locale-aware, but it's easy enough to find the decimal point and thousands separator characters should they be needed (and there are alternatives, like the Intl extension's NumberFormatter). Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408244 Share on other sites More sharing options...
Jessica Posted January 25, 2013 Share Posted January 25, 2013 The folks of Londonshire also use those particular characters. The number_format() function is not locale-aware, but it's easy enough to find the decimal point and thousands separator characters should they be needed (and there are alternatives, like the Intl extension's NumberFormatter). Ah, who uses them the other way around? I assumed since op was only showing numbers without a decimal point that it was the other way. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408256 Share on other sites More sharing options...
devilsvein Posted January 25, 2013 Author Share Posted January 25, 2013 Sorry for unsolving this. It seems it didnt work -.- @jessica when the number_format does work it displays the commas and dots in the right place. Its only this particular column name which is causing issues. Even after i delete and readd it in phpmyadmin. I've saved it with decimal (11,2) and in the database it clearly shows the right amount with the decimal point followed by 2 digits. I get my number and store it in a variable like this $query = "SELECT * FROM users WHERE PlayerID='".$_SESSION['ID']."' LIMIT 1"; $result = mysqli_query($mysqli, $sql); $row = mysqli_fetch_assoc($result); $m = $row['money']; Even that the $m shows the number but no decimal.... I also have this in my code on another page. I think this might cause issues $m = "£" . $m; but even after commenting that out I still dont get the decimal points in. As mentioned earlier, if I bring into play number format only the first digit shows.. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408273 Share on other sites More sharing options...
kicken Posted January 26, 2013 Share Posted January 26, 2013 (edited) var_dump($row['money']); right after you fetch it from the database and see what it says the value is. Post it here. Edited January 26, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408302 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 (edited) Thanks for that kicken. Var dump totally didn't come to mind... It returns null strangely. Sorry Ignore the above. I looked at it again and it showed the below: string(7) "2500.00" Im guessing that the string is the problem as it should be float? Edited January 26, 2013 by devilsvein Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408392 Share on other sites More sharing options...
Jessica Posted January 26, 2013 Share Posted January 26, 2013 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408394 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 (edited) So i've done this: $m = (float) $row['money']; and var dump of $m produces => float(2500.54) however when I echo $m to screen without any further changes I get 2501 Edited January 26, 2013 by devilsvein Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408396 Share on other sites More sharing options...
Jessica Posted January 26, 2013 Share Posted January 26, 2013 Did you try the number format again? What is the number supposed to be? Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408399 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 The final result should display 2 decimal places for example 250.54 When I apply number format and carry out a var dump it shows the float number back as being a string. It also only shows digits before the comma in number format Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408400 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2013 Share Posted January 26, 2013 You have to show us your exact code that reproduces the problem, from the point where you are retrieving/var_dumping the value, through to the code that is displaying the incorrect value, if you want us to do more than offer guesses as to what is causing the problem. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408402 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 Ok starting with the database column Its called money and is a decimal(11,2). Null is no. In the database php page I have: $sql = "SELECT * FROM users WHERE PlayerID='".$_SESSION['ID']."' LIMIT 1"; $result = mysqli_query($mysqli, $sql); $row = mysqli_fetch_assoc($result); $m = (float) $row['money']; var_dump($m); When its displayed its usually done by echo $m; Sometimes this is in a function like so... function Banner() { global $m, $g ; //$m = "£" . $m; //some code here... echo "</td></tr> <tr><td><small>Money:</small></td><td width=80px>"; echo $m; //some code here..... } Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408405 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2013 Share Posted January 26, 2013 The code you posted, for a starting value of 250.54, outputs Money:250.54 or Money:£250.54, depending on if the line that concatenates the £ symbol is being used. Therefore, that's not the code that reproduces the problem. Let me put this another way, until you post actual code that reproduces the problem, you are not going to get a solution. You are doing something that doesn't work and the only way we can help is if you show us the code that you need help with. If you had done so yesterday when you started this thread, you could have had this solved yesterday. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408413 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 shows 250.54 Thats all the code that relates to £m ? I don't know what else to post... Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408414 Share on other sites More sharing options...
Barand Posted January 26, 2013 Share Posted January 26, 2013 (edited) I see you using globals . Do you perhaps have a sequence of events like this <?php function formatAmount() { global $m; $m = number_format($m); } $m = 1234.56; echo $m; // 1234.56 echo '<br>'; formatAmount(); echo $m; // 1,234 echo '<br>'; formatAmount(); echo $m; // 1 ?> Edited January 26, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408415 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 I see you using globals . Do you perhaps have a sequence of events like this <?php function formatAmount() { global $m; $m = number_format($m); } $m = 1234.56; echo $m; // 1234.56 echo '<br>'; formatAmount(); echo $m; // 1,234 echo '<br>'; formatAmount(); echo $m; // 1 ?> I wouldn't have thought it could have been that but I did have a look. Below is the exact function of my code function Banner() { global $userid, $username, $level, $m, $g, $brave, $donator, $perc_energy, $shooting, $maxshooting, $perc_concen, $brave, $maxbrave, $perc_health ; //$m = "£" . $m; $d = $donator; if ($d >=1 ) { $d = "<img src='donator/donator.gif' width='12px' height='13px' title='You have $donator days remaining' />"; $username = "<font color='#4a3401'>$username</font>"; } else { $d = ""; } echo "<div class=bannimage> <img src='gameimage/newbann.png' /> <div class=text> <h3 style=margin-top:0px; margin-bottom:0px;>"; echo "["; echo $userid; echo "] " ; echo $username; echo $d; echo "</h3> <table cellspacing=0 cellpadding=0 width=190px border=0> <tr><td><small>Level:</small></td><td width=80px>"; echo $level; echo "</td></tr> <tr><td><small>Money:</small></td><td width=80px>"; echo $m; //Here we get 2,501 displayed.....its rounded up and not showing decimal echo "</td></tr> <tr><td><small>Gold:</small></td><td width=80px>"; echo $g; echo "</td></tr> <tr><td><small>Faction:</small></td><td width=80px>"; echo $brave; echo"</table> </div> <div class=stats> <table cellspacing=0 cellpadding=0 width=190px border=0> <tr><td><small>Energy:</small> </td><td width=80px>$perc_energy%</td></tr> <tr><td><small>Shooting:</small> </td><td>$shooting / $maxshooting</td></tr> <tr><td><small>Concentration:</small> </td><td>$perc_concen%</td></tr> <tr><td><small>Brave:</small> </td><td>$brave / $maxbrave</td></tr> <tr><td><small>Health:</small> </td><td>$perc_health%</td></tr> </table> </div> </div>"; } That $m is also displayed out of the function such as tables as echo $m; Again, same result. this is from the array in my database $m = (float) $row['money']; I have realised that the output has commas in it as if its already been number_format when I haven't placed any number formats. the database doesn't have a comma in. So i think if there's nothing wrong in the code I have, something somewhere is formatting my number and I think im trying to overide it. Im going to go through all my pages. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408430 Share on other sites More sharing options...
devilsvein Posted January 26, 2013 Author Share Posted January 26, 2013 Yeah there was another number_format in another page which was trying to format the same variable. I think its best i apoligise to use all for wasting your time on a stupid mistake I did. Really am. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408431 Share on other sites More sharing options...
PFMaBiSmAd Posted January 27, 2013 Share Posted January 27, 2013 In case someone didn't state it already, you should NOT use the global keyword to bring data into a function, ever. For this exact reason. You must dedicate that variable to that specific purpose and can not reuse it anywhere else in the entire application for any other purpose. It just takes more time when writing the code, because someone must keep track of all of the variable names and it takes a huge amount of time debugging anything that goes wrong. Data should only be passed into functions as call-time parameters. Quote Link to comment https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/#findComment-1408451 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.