Jump to content

[SOLVED] number format gbp


fazzfarrell

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;

Archived

This topic is now archived and is closed to further replies.

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