Tuesday, 7 October 2014

number format in php



number_format in PHP:




<?php
$subTotal = "1,233.00";
$subTotal = number_format($subTotal, 2);
echo $subTotal;  //1.00  with Notice: A non well formed numeric value encountered

?>
 

 Reason for above notice:
number_format() works on numeric data types, not on formatted strings.... if you provide a formatted string to number_format() then it will be cast to a numeric using normal PHP casting rules (as defined in the documentation), which means that anything after a comma will be ignored.

Because PHP treats that value as a string. But number_format() wants an integer. So it converts that value to an integer. But that means truncating the number at the first non-digit it finds which is the comma. 

Solution:
<?php
//Remove the commas before you call number_format().

$subTotal = "1,233.00";
$subTotal = number_format(str_replace(',', '', $subTotal), 2);
echo $subTotal;  //1,233.00
 ?>


If you want 35679 to show up as 35,679:

number_format(35679,0,'',',');

First parameter is the input number.

Second is the amount of decimals.

Third is the decimal separator (not needed without decimals).

Last is the thousands separator.


Required output: 4598.362
<?php

$num = 4598.3622353;
$req_num = number_format($num, 3, '.', '');
echo $req_num; 




$req_num = number_format($num, 2, '.', '');
echo $req_num;  //4598.36

---------------------------------------------------------------- --------------------------
//I have number 55 and required output is 2.7 AND I need to output 2.7 instead ( 55/2 -> 27.5 -> 27 -> 2.7 )
 
$your_final_number = 2.7;

echo number_format($your_final_number, 1); 
------------------------------------------------------------------ ---------------------

$foo = "105";
 echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
----------------------------------------------------------------------------------------------
$unpadded = 520;
$padded = sprintf('%0.2f', $unpadded);
echo "<br>".$padded;
// 520 -> 520.00
----------------------------------------------------------------------------------

Having the following decimal numbers:
47.44
180.11
340
12.39
25

how can I add a default .00 to those numbers, that have no decimals?


echo sprintf('%0.2f', $val)

echo "<br>Another way:";

echo number_format($number, 2);



echo round(520.34345,2); // 520.34
echo round(520, 2); // 520








echo intval(3.4); //3

echo round(3.4); //3










echo "<br>";

echo intval(3.9); //3
echo round(3.9);  //4


echo round(4.3924256231, 2);    //4.39
echo round(4.3224256231, 2);    //4.32



-----------------------------------------------------------------------------

?> 

Example #1 
round() examples
<?php 
echo round(3.4); // 3 
echo round(3.5); // 4 
echo round(3.6); // 4 
echo round(3.6, 0); // 4 
echo round(1.95583, 2); // 1.96 
echo round(1241757, -3); // 
1242000 echo round(5.045, 2); // 5.05 
echo round(5.055, 2); // 5.06 
?>

Example #2 

mode examples
<?php 
echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10 
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9 
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10 
echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9 
 echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9 
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8 
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8 
echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9 
?>

-------------------------------------------------------------------------------------------------------------------------------


Supplier's price: $1854.81 and the required format is: $1854.99

<?php

$preciosinr = 1854.81;
$price=number_format(round($preciosinr,0)-0.01,2,'.','');

echo $price;

?> 






0 comments:

Post a Comment