I'm trying to output a value from xml this is what I do -
<?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 isn't it displaying as 9? Thanks.
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2;
?>
Try casting the value to a numerical value first.
$num = (double) $responseTemp->Items->Item->CustomerReviews->AverageRating;
echo $num * 2;
See Type Juggling and String Conversion to Numbers on the PHP website for more information.
If you are looking for a decimal value without doing typecasting, you have to multiply by a number with a decimal. Otherwise it will return a regular integer like the number you gave it.
Try multiplying by 2.0
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0;
Related
I am trying to create an ecommerce store and our prices need to fluctuate with the exchange rate for different countries so I'm dealing with a lot of decimal places.
What I want to do is round the original price to the nearest full number (as in they can keep the change). But then I want to format that as a currency with two decimal places.
<?php
$number = 12345.6789;
echo $number; // outputs '12345.6789'
$number = number_format($number,0);
echo $number; // outputs '12,346'
$number = number_format($number,2);
echo $number; // outputs '12.00'
?>
After formatting to no decimal places it starts reading the ',' as the decimal separator instead of the thousands separator and formats that for two decimal places.
It also gives the following error:
A non well formed numeric value encountered in C:\wamp64\www\Lifting365\test.php on line 6
How can I achieve what I am looking for?
As specified in the documentation, number_format returns a string value, you can't reuse it as a number.
Use the function round() to round your number, if you want to round it to the direct upper integer use ceil() instead.
number_format(round(12345.6789), 2);
// apply intval to get the low integer value (for change purposes)
$number = 12345.6789;
echo $number; // outputs '12345.6789'
echo intval($number)."<br/>"; // outputs '12345'
echo number_format(intval($number),0,'.','.'); // outputs '12.345'
echo number_format(intval($number),0,'.',','); // outputs '12,345'
Use round function and then number_format.
// returns 12,346.00
number_format(round(12345.6789), 2);
The function number_format accepts 4 parameters. Per default a point will be used as decimal seperator and comma as thousands seperator (12345.6789 become 12,346 after your first call; as excepted). It's not explicitly documented but number_format also rounds.
http://php.net/manual/de/function.number-format.php
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
You are getting an error because you reuse the same variable $number. After your first call to number_format you dont have a float value anymore.
<?php
$number = 12345.6789;
echo $number."<br>"; // outputs 12345.6789
echo number_format($number,0)."<br>"; // outputs 12,346
echo number_format($number,2)."<br>"; // outputs 12,345.68
?>
If you are not sure what is in your variable you can apply floatval to it.
echo number_format(floatval($number),2);
The PHP function that you're looking for is money_format() http://php.net/manual/en/function.money-format.php have a good read through the manual page (including the comments)
I have looked for a way to do this and have not found it.
I have values read from MySQL: 100.00, 85.50, 97.00, 71.33
I want them to display as: 100, 85.5, 97, 71.33
I see number_format() that specifies FIXED decimal places, but I need a sort of 'significant digits format'
use (float)$number;
$a = '100.00';
$b = 73.50;
$c = 71.33;
echo (float)$a; // 100
echo (float)$b; // 73.5
echo (float)$c; // 71.33
you need to use floatval function to get your required output. just check below code.
var_dump(floatval('100.00'));
var_dump(floatval('85.50'));
var_dump(floatval('71.33'));
I have this problem:
// 7788778877887786660462040644602088666448 (10) = 9mwjmtyko1mpect1brz2exoncc (36)
$a = 7788778877887786660462040644602088666450 - 2;
echo $a."<br>"; // prints 7.7887788778878E+39
$b = "9mwjmtyko1mpect1brz2exoncc";
echo base_convert($b, 36, 10)."<br>"; //prints 7788778877887786660462040644602088666448
I know why $a was printed to 7.7887788778878E+39, it's because the value of $a is over the integer limit.
But, why it is not being 7.7887788778878E+39 when I convert 9mwjmtyko1mpect1brz2exoncc to decimal? It just prints the exact value (7788778877887786660462040644602088666448)
How can I print $a to the exact value (ignoring error)?
Is 7788778877887786660462040644602088666448 another data type that is bigger than long/unsigned-long?
Looking at some of the answers on this page:
How to keep long int during PHP string conversion?
I don't think you can do that because the large values are being stored as a float.
Suggestions are to use a library like BC Math and GMP, but I think that those are not enabled in PHP by default.
Hi i need to save a 010 number in $number and if i do like this php will remove the starting 0
$number = 010
And echo of this will return 10 how can i make it not to remove the initial 0
BR
Martin
Use it as a String:
$number = '010';
Use str_pad() function.
echo str_pad('10',3,'0',STR_PAD_LEFT)
http://php.net/manual/en/function.str-pad.php
Do remember that numbers starting with 0 can also be treated as octal number notation by the PHP compiler, hence if you want to work with decimal numbers, simply use:
$num = '010';
This way the number is saved, can be stored in the database and manipulated like any other number. (Thx to the fact that PHP is very loosely typed language.)
Another method to use would be:
Save number as $num = 10;
Later while printing the value you can use sprintf, like:
sprintf("%03d", $i);
This will print your number in 3 digit format, hence 0 will be added automatically.
Another method:
<?php
$num = 10;
$zerofill = 3;
echo str_pad($num, $zerofill, "0", STR_PAD_LEFT);
/* Returns the wanted result of '010' */
?>
You can have a look at the various options available to you and make a decision. Each of the method given above will give you a correct output.
I am getting the following output correctly:
<?php echo number_format("12312.312","1"); // Correct Output 12312.3 ?>
but in the following case
<?php echo number_format("12312","1"); // Getting output 12312.0 but requires only 12312 ?>
So basically, I want to control my output i.e. it should add decimal place only if my decimal digit is greater than 0.
The second parameter for number_format() takes the number of decimals - so your example is the the expected result. Maybe you are intereseted in the round() function which allows to round to a certain precision?
You can try something like this
<?php
$number = 12312;
echo is_int($number) ? $number : number_format($number,"1");
?>
If you don't want the extra decimal place, use <?php echo number_format(12312, 0);?>
The "0" represents 0 decimal places
simple casting it to float, usign (float), works.
e.g.
$num_1 = (float)1.0;
$num_2 = (float)1.1;
echo $num_1;
echo $num_2;
output:
1
1.1