PHP Number Format

I have the following PHP Mathematical equation:
	
<?php
	$bv = 16150;
	$div = 100;
	$br = 6.30;

    $rate = $bv/$div;
	$sub_price = $br * $rate;
	$html = number_format($sub_price, 2, '.', ',');

	echo $html .'<br />';
	if ($html > 100) {
	echo $html;
	}			
?>

Open in new window


The $html in this case is 1,017.45
The if $html > 100 will not work.
If I adjust the $bv so that the $html figure is less than 1000 it does work.  What am I doing wrong?
LVL 7
rgranlundAsked:
Who is Participating?
 
Chris StanyonCommented:
The reason your code is not working when $html is above a thousand, is because the $html string then contains a comma, which prevents PHP from automatically casting the string to a number. If $html is less that 1000, it doesn't contain the comma and everything works.

An alternative to checking against $sub_price is to strip out the comma before checking if it's greater than 100, so that PHP can cast it to a number:

if (str_replace(",", "", $html) > 100) {
	echo $html;
}

Open in new window

0
 
Chris StanyonCommented:
You should be checking whether $sub_price is greater than 100:

if ($sub_price > 100) {
    echo $html;
}

Open in new window


$html is a string - $sub_price is the actual number.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.