So when I try the following:
$a = '1.00';
$b = '1.01';
if($a < $b){
print 'ok';
}
This works fine. But when I retrieve these variables from an xml file. the strings are EXACTLY the same, but for some reason, the if function doesn't work right. So I assume I have to convert the strings to a number. But when I do so, the decimals get removed.
Is my assumption correct? If so, how do i solve it?
If not, what's the problem?
Thank you!
$a = (float) $a;
$b = (float) $b;
Related reading: http://php.net/manual/en/language.types.type-juggling.php
Found the answer by googling :)
floatval($string);
Related
PHP
<?php
$a = $offer->service_original_price_display;
$b = $offer->service_discounted_price_display;
$c = $a - $b;
?>
However I am getting this error: A non-numeric value encountered since service_original_price_display is '$500' and service_discounted_price_display is '$300'. I assume it is because both contains the dollar sign hence PHP is not able to perform the equation. Is there any solution to this? Thanks!
$a = str_replace("$", "", $offer->service_original_price_display);
$b = str_replace("$", "",$offer->service_discounted_price_display);
$ is string
use trim($a, '$'); to remove both side $ sign
You can use substr, if you know the first place is always occupied by a $ from an error standpoint, removing the $ ( with str_replace ) is probably better. But, as those answers where already posted. I get the scraps ( lol ).
$a = '$500';
$b = '$300';
$c = substr($a,1) - substr($b,1);
echo $c;
Never had to do such a basic thing before. How do I initialize a variable as a float before doing logic? Do either of these examples make sense?
$var = 0.0;
$var = (float) 0;
Both make sense, the first is shorter, with the second you can initialized with a dynamic value, your choice !
EDIT : But I agree with #Niet the Dark Absol, it poorly matters since you use test function such as ctype_digit, or is_numeric.
from http://php.net/manual/en/language.types.float.php
Floating point numbers (also known as "floats", "doubles", or "real
numbers") can be specified using any of the following syntaxes:
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
so defining a float by using $var = 0.0 is correct. While casting is not technically incorrect I don't believe it's something you should use when you don't need to.
Try to use number_format.
For example:
$number = 20;
echo number_format($number,2) // will output 20.00
// number_format($number,$decimals,$dec_point,$thousands_sep)
I need to add numbers in php without changing the number format like below
$a = "001";
$b = "5";
$c = $a+$b;
Now the result comes like "6" but I need "006" if $a is "01" then the result should be "06".
Thanks
Technically speaking, the $a and $b in your example are strings - when you use the addition operator on them they converted to integers which can't retain leading zeroes. More details on string-to-number conversion are in the manual
Something like this would do it (assuming positive integer strings with leading zeros)
#figure out how long the result should be
$len=max(strlen($a), strlen($b));
#pad the sum to match that length
$c=str_pad($a+$b, $len, '0', STR_PAD_LEFT);
If you always know how long the string has to be, you could use sprintf, e.g.
$c=sprintf('%03d', $a+$b);
Here, % introduces a placeholder, 03 tells it we want zero padded to fill at least 3 digits, and d tells it we're formatting an integer.
Hope this would help you:
<?php
$a="001";
$b="5";
$l=max(strlen($a),strlen($b));
$c=str_pad($a+$b, $l,"0", STR_PAD_LEFT);
echo $c;
?>
For common case. Your code should looks like this.
$a = someFormat($original_a);
$b = someFormat2($original_b); // $b has different format.
$c = someFormat($a + $b);
Or, you need write formatRecognition function.
$a = getValueA();
$b = getValueB();
$c = someFormat(formatRecognition($a), $a + $b);
<?php
$a = "time(0)";
$b = eval($a);
Like this, I want to put the result of the data....
I can put the string with eval code.. But how can I do this?
1) your string must contain completed php code, ie has ; at the end
2) code in the string doesn't produce any output and it results in NULL value of $b
So, to make it working, write:
$a = "time(0)";
$b = eval("echo ".$a.";");
echo $b; // 1433582861
I think this will help you,
$a = "time(0)";
$b = call_user_func($a);
$c = new DateTime($b);
var_dump($c);
Consider the below code in PHP
$a = 8425996523 * 121212713;
$b = sprintf('%.2f', $a);
$mul = gmp_mul("8425996523", "121212713");
Output is
1.0213378982814E+18
1021337898281396864.00
1021337898281396899
The actual answer is 1021337898281396899. Hence, it is clear that we need to use any libraries like gmp_mul to do arithmetic with large numbers.
My question is, how to identify such errors?
ie, when PHP does a calculation like
8425996523 * 121212713
, how can I identify that the result is not correct?
One way I see is to check for E and assume that whenever we forcefully convert such numbers, the errors exits.
Use
$b = sprintf('%.2d', $a);
Instead of
$b = sprintf('%.2f', $a);
Here's a screenshot of the source code to show the difference: