Php Calculation [duplicate] - php

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

Related

How to show only two numbers after decimal using php eval() or round()? [duplicate]

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

reverse chunk split of integer in PHP [duplicate]

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);

How to get double value in php [duplicate]

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);
?>

How to get not so many digits in PHP [duplicate]

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));

PHP number format drops zero when rounding [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
I'm using PHP's number_format to display floats to 2 decimal places.
When the number is something like 1.898 it gets rounded up to 1.9.
How do I get it to display that as 1.90?
Update:
I have a function that ends...
return number_formant($num, 2);
The php script that calls the function prints out the number to be used by Javascript. When I do a var_dump on the number, it prints correctly with two decimal places. Looks like it's Javascript that's loosing the zero.
Here's the JS code that was causing the issue...
function show_level(level) {
...
if (level > 9999)
level_label = (level / 1000).toPrecision(3) + 'k';
else if (level > 999)
level_label = (level / 1000).toPrecision(2) + 'k';
else
level_label = level;
I altered the last line to get it working how I wanted..
level_label = level.toFixed(2);
Maybe not the best solution but:
$n = 1.2345;
$n = number_format( round($n, 1), 2);
//echo 1.20
echo $n;
So,
you mean something like this (what you have):
<?php
echo number_format($x);
?>
and here is what you want:
<?php
echo number_format($x,2);
?>
you can use other lengths. Try it Out!
Hope could helpya
:)
echo sprintf('%01.2f', number_format(1.898,2))
You can do:
echo round(1.898,2);

Categories