Jump to content

Recommended Posts

My code

 

<?php
		 //Rob Farrell calculate cost - %
$english_format_number = number_format($number);
        $number1 = $HTTP_SESSION_VARS["icPoles"]->col("Total");
if (isset($_SESSION['MM_Username']))
{
	$number = ($number1 / $row_rsUser['Percent'] * 100);
}else{
	$number = $number1;
}
             echo number_format ($number, 2, '.', ' ');
?>

 

gives the result

 

Total:    £789.23

 

You Pay (including Delivery)  £ 671.69

 

which is correct, if I go over a £1000 it gives this for example:

 

Total:    £1,249.23

 

You Pay (including Delivery)  £ 0.85

 

Any one see why?

 

 

Link to comment
https://forums.phpfreaks.com/topic/53735-solved-number-format-gbp/
Share on other sites

Uh, yeah.  That's probably why it's not working.  The only difference between a number under a thousand and over that might cause that kind of error would be a comma.  Despite looking like a number to you or me, computers don't typically interpret numbers containing commas as numbers, but as strings.  If you know how PHP tries to convert strings to numbers, you can predict that it will try to convert the string "1,000.00" to a number by taking all the leftmost numbers until it hits a non-number or non-decimal, in this case the comma.  That means PHP converts "1,000.00" to 1.

 

Seriously, check your variables within your calculations; see if there isn't a comma in the value.

Do this for me.  Execute this code in place of your posted code above and post the output.

 

<?php
$english_format_number = number_format($number);
$number1 = $HTTP_SESSION_VARS["icPoles"]->col("Total");
if (isset($_SESSION['MM_Username'])) {
echo "\$number1 = $number1<br>\n\$row_rsUser['Percent'] = $row_rsUser[Percent]<br>\n";
$number = ($number1 / $row_rsUser['Percent'] * 100);
} else {
echo "\$number1 = $number1<br>\n";
$number = $number1;
}
echo number_format ($number, 2, '.', ' ');
?>

i get this:

 

 

£ $number1 = 1,249.23

$row_rsUser['Percent'] = 117.5

0.85

 

So it's like I suspected, $number1 is a string containing non-number characters, and you'll have to remove them before using it in a calculation (or at least if expecting it to behave like 1249.23).

 

Make this change:

 

if (isset($_SESSION['MM_Username'])) {
$number = floatval(str_replace(',','',$number1)) / $row_rsUser['Percent'] * 100;

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.