PHP calculation

I am trying to infer a value from xml, this is what I am doing -

<?php echo $responseTemp->Items->Item->CustomerReviews->AverageRating; ?> 

This outputs 4.5, but when I change it to the code below, it displays as 8. Why doesn't it display as 9? Thanks.

 <?php echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2; ?> 
+4
source share
2 answers

Try to print the value into a numerical value first.

 $num = (double) $responseTemp->Items->Item->CustomerReviews->AverageRating; echo $num * 2; 

See Type Juggle and Convert Strings to Numbers on the PHP website for more information.

+6
source

If you are looking for a decimal value without casting, you need to multiply by a number with a decimal point. Otherwise, it will return the correct integer, such as the number you gave it.

Try to multiply by 2.0

 echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0; 
+2
source

All Articles