This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 10 months ago.
Actually, I want to add two number and the result should be in double.
<?php
$a=4.0;
$b=4;
echo $a+$b
?>
expected output will be 8.0 but it gives result 8. I'm new to PHP.
Actually it's worked correctly and $a+$b is float, but because it's 8.0, showing 8. You can use number_format() function to add fixed decimal point to numbers.
<?php
$a=4.0;
$b=4;
echo $a+$b// Ouput: 8
var_dump($a+$b); //Output: float(8)
echo number_format($a+$b, 1); //echo with one decimal: 8.0
?>
You can test it with other numbers:
<?php
$a=4.1;
$b=4;
echo $a+$b// Ouput: 8.1
var_dump($a+$b); //Output: float(8.1)
echo number_format($a+$b, 1); //echo with one decimal: 8.1
?>
Try this
<?php
$a=4.0;
$b=4;
echo sprintf("%.2f", $a+$b);
?>
Related
This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example
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:
Weird PHP behavior: won't assign the integer 8 to a variable
(5 answers)
Closed 4 years ago.
0621 - is STD code
22465874 - is phone number.
I can access phone number correctly but using $associativeArray[0][2][0] gives wrong value.
<?php
error_reporting(0);
echo "<br>";
$associativeArray = array(
array(1, "Rahul", array(0621, 22465874)),
array(2, "Ayushi", array(0261, 2265471)),
array(3, "Ritik", array(2314, 4789556))
);
echo $associativeArray[0][2][0], "<br>";
echo $associativeArray[0][2][1];
?>
If a number start with a zero, it is interpreted as an octal number by PHP.
So you simply have to put your phone numbers in quotes to avoid problems:
<?php
error_reporting(0);
echo "<br>";
$associativeArray = array(
array(1, "Rahul", array('0621', '22465874')),
array(2, "Ayushi", array('0261', '2265471')),
array(3, "Ritik", array('2314', '4789556'))
);
echo $associativeArray[0][2][0], "<br>";
echo $associativeArray[0][2][1];
?>
Output:
<br>0621<br>22465874
Demo: https://3v4l.org/c48jl
This question already has answers here:
Remove useless zero digits from decimals in PHP
(29 answers)
Closed 6 years ago.
javascript Number function is handy when dropping undesired zero if decimal part is zero e.g
Number(2.00)
2
Is there such function in php or any alternative
I think this works that way by default in PHP. If you use proper number type like float or double.
If you're using string then you need to map
$a = '2.00';
echo (float)$a; // 2
Example of using float
$a = 2.00;
echo $a; //2
or
2.00 + 0; //2
If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)
$a = 2.00
echo number_format($a, 2); // 2.00
You can achieve this by adding zero ... for example
echo "Result " . (2.00 + 0) . "\n";
Result 2
you can use floatval()
echo floatval('2.00'); //2
echo floatval('2.23'); //2.23
See the documentation for more info
you can use intval() or any roundoff function
$_float = 1.9020441;
$_float = explode(".", $_float);
echo strlen($_float[1]);`
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