Jump to content

Recommended Posts

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 by devilsvein
Link to comment
https://forums.phpfreaks.com/topic/273643-php-number_format-being-silly/
Share on other sites

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 by Jessica

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?

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 by devilsvein
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).

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.

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..

 

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 by devilsvein

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 by devilsvein

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

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.

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.....
}

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.

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 by Barand

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.