This question already has answers here:
How do I truncate a decimal in PHP?
(6 answers)
Closed 10 months ago.
<?php echo (($priceInt[0]-$specialInt[0])/$priceInt[0])*100 ?>
For every price this gives me something like 11,111111111 or 3,3433333333
How can i limit it so i will get only 11 or 3 ? Without anything after " , ".
Take a look at http://php.net/manual/fr/function.number-format.php
Use it like this :
echo number_format((($priceInt[0]-$specialInt[0])/$priceInt[0])*100, 0, '', '');
If you just need the integer part just use :
echo intval((($priceInt[0]-$specialInt[0])/$priceInt[0])*100);
You can use sprintf() like below:
echo sprintf("%d",'3,3433333333'); //output : 3
or
echo sprintf("%d",'11,111111111'); //output : 11
In your case:
echo sprintf("%d",((($priceInt[0]-$specialInt[0])/$priceInt[0])*100));
Thanks a lot guys i fixed it
echo sprintf("%d",((($priceInt-$specialInt)/$priceInt)*100));
Related
This question already has answers here:
Adding string with number in php
(6 answers)
Closed 2 years ago.
I'm a beginner in PHP. with my experience something like "number + String + number" should be a string.
But apparently, it's a number in php.
in my case:
echo 14 + ' ' + 12;
// returns 26
// expected: "14 12"
why did this happen?
and how to fix it?
You are adding a '+' for adding two numbers. These are not a string so it will do a addition.
Use this if you want to add these couple of number as a couple of string :-
echo '14'.' '.'12';
// result "14 12";
You can also use with a html code for a blank space :
echo '14 12';
// result "14 12";
Because echo accepts parameters only inside quotes. If you want text being returne use:
echo"14 12";
In you case PHP does 2 things
Takes 14 adds to 12
Concatenates space.
This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);
This question already has answers here:
Convert exponential number presented as string to a decimal
(3 answers)
Closed 5 years ago.
I am trying to convert a scientific notation/exponential number.
For example :
I get from a API request this number : 4.96E-6
I want to convert it to his normal value 0.00000496.
I searched on the web and tryed a few codes like
$awb = "4.96e-6"; // tried with and without quotes
$format = sprintf($awb, "%e");
var_dump($format);
it returns : string(7) "4.96E-6"
$awb = 4.96e-6;
$format = sscanf($awb, "%e");
var_dump($format);
Is returning :
array(1) { [0]=> float(4.96E-6) }
Please what should I do ?
Thank you
You can use the number_format() function; e.g.
<?php
$awb = "4.96e-6";
print number_format($awb, 10);
?>
Will result in the output: 0.0000049600.
See: http://php.net/manual/en/function.number-format.php
This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
I've just started learning php an hour ago. I've made this code:
$x=2;
$y=4;
echo $x;
echo $y;
if($x=5)
{
echo "$x";
}
else
{
echo "test";
}
I'm expecting the output: 24test
I'm getting the output: 245
x equals 2 in the beginning. Why is x then changing to 5 when the only thing I do is CHECKING whether x = 5?
I've searched the web and this site for an answer but couldn't find anything. Thanks in advance!
Tony
Note that = is use to assign value while == use to evaluate value within if statement so your code should be
if($x==5) {
Instead of
if($x=5) {
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
I am making a simple order page the user will select an ammount on 1 page then on the next page it runs this
<?php
$bar = $_POST['bar'];
$sum= $bar * 1.59;
echo "Your price". $sum;
?>
the only problem we are having with this is that if they enter a certain ammount say 10 so for example 10 * 1.59 = 15.90 but it only shows 15.9 for any others it will show the ammount fully e.g for 5 it would show £7.95 - Is there something i could change in the code for it to always show the xx.xx figure instead of xx.x
thanks
I think that this is exactly what you're looking for: http://www.php.net/manual/en/function.money-format.php.
<?php
setlocale(LC_MONETARY, 'en_GB');
...
echo "Your price: " . money_format("%.2n", $sum);
?>
<?php
echo sprintf("Your price %.2f", $sum);
?>
You may use PHP function number_format.
<?php echo "Your price". number_format(number, 2, '.', '')); ?
http://php.net/manual/en/function.number-format.php