PHP zeropad (sprintf) AND number_format total [duplicate] - php

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
I have a $total with a value of 1400 which I'm trying to echo as 1,400.00 however sprintf and number_format refuse to cooperate with each other. How do I properly format the $total to echo as 1,400.00?
<?php
$total = 1400;
echo sprintf('%0.2f',$total);
//'1400.00'
echo number_format($total);
//'1,400'
echo sprintf('%0.2f',number_format($total_grand));
//'1.00'
echo number_format(sprintf('%0.2f',$total));
//'1,400'
?>

This should suffice:
$total = 1400;
echo number_format($total,2);
This will output:
1,400.00
See: number_format()

Related

cannot compare to float value in php [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have got following code in php:
$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
echo $remainingRest;
if( $remainingRest == 0.02 )
echo "equal";
else
echo "not equal".$remainingRest;
nothing more, it should print equal but somehow it's printing not equal, through I see, $remainingRest is equal 0.02.
Thanks for help in advance
Returns false because float values ​​don't equal exactly when equalize.
You can try to equalize;
<?php
$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
if(round($remainingRest,2) == round(0.02,2))
echo "equal";
else
echo "not equal".$remainingRest;
?>

Can not get proper result for type casting in PHP [duplicate]

This question already has answers here:
How to convert float value to integer in php?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have a Float variable
$float = 37.80
Then I multiply the float by 100:
$mul = $float *100
Now I have 3780.
$typecasting = (int) $mul;
Now when I print $typecasting, it shows 3779
For example:
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = (int) $mul;
echo $typecasting;
// $typecasting shows 3779
I can't understand, how it works.
This problem occurs only for 32.80, 33.80, ...4.80. and also for multiples of 2, i.e. 65.60.
Just use floatval() http://php.net/manual/en/function.floatval.php
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = floatval($mul);
echo $typecasting;
// $typecasting shows 3779

PHP Increase Letter Works, Decrease Does Not [duplicate]

This question already has answers here:
Decrementing alphabetical values
(3 answers)
Closed 7 years ago.
I have the following code.
$letter = 'D';
$letter++;
echo $letter; //outputs E
$letter--;
echo $letter; //outputs E
Why isn't the subtract function working?
You should make use of ord() and chr() functions:
$letter = 'D';
$val = ord($letter);
$val++;
echo chr($val);
$val--;
echo chr($val);

PHP output string with variable? [duplicate]

This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy

convert a number to the next 10,20,30 and so on [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php nearest ten-th value
i want a function simillar to ceil but to get the next 10,20,30,40,50,60,70,80,90,100 number
example:
$x = 5;
echo theunction($x);
returns 10 and if $x = 15.5; it should returns 20 and so on
Simply Try
$x = ceil($x/10) * 10;

Categories