I am using php. I want to display a number in a '$XX.XXX.XX' format. I tried the following code, but its not giving full output.
<?php echo "$".number_format(round($customer['hourly_payment']),2); ?>
Output
$17,317.00
Please suggest a solution!
I believe you missed the following function's last 2 parameters:
string number_format ( float $number , int $decimals = 0 , string
$dec_point = "." , string $thousands_sep = "," )
For your case:
number_format($number, 2, '.', '.');
Read more: http://tr1.php.net/number_format
Try with -
echo number_format(100000000, 2, '.', '.');
Output
100.000.000.00
Related
I'm trying to fetch data from a database, using json. but the data retrieve from the database is of type String which is causing an error.
What I want to do is to format the data to a float type with 2 decimal places.
I have gone through similar probable answers, but none seems to solve the challenge.
This is what I have done.
$num1 = "9";
//Using number_format
echo (float) number_format($num1,2,'.',''); // result is 9 instead of 9.00
//Using Floatval
echo floatval("9"); // result is 9 instead of 9.00
How can I get the result formatted as 9.00 ?
You need to put the (float) inside the number_format.
echo number_format((float) $num, 2, '.','');
try this: echo number_format ((float) $num1, 2, '.', '');
the format of this is: string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) from http://php.net/manual/en/function.number-format.php
another method is: sprintf('%0.2f', $num1);
and sprintf is used to format strings and might be applicable in other areas as well. http://php.net/manual/en/function.sprintf.php
I try to use php number_format to show number like this xxx,xxx,xx.xx .
When I echo value it auto increment from 9999999 to 10000000.
Anyone has idea how to show data result 9,999,999?
<?php echo number_format(99999999,2); ?>
String number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
So, with your example:
<?php echo number_format(9999999, 2, '.', ','); ?>
Outputs:
9,999,999.00
Or if you did not want the decimals:
<?php echo number_format(9999999, 0, '.', ','); ?>
Outputs:
9,999,999
From php.net:
This function accepts either one, two, or four parameters (not three)
Just a reminder.
I need to represent 2.5 into exact 2.50 using php. I have used
sprintf('%0.2f',2.5)
But it did not work. What can be the possible answer?
I have used many round off functions too like
round()
But I didn't get solution. Please help me.
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$number= "2.5";
echo number_format((float)$number, 2, '.', ''); // Outputs -> 2.50
This function returns a string.
You can use
string number_format ( float $number [, int $decimals = 0 ] )
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
$foo = "2";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 2.00
The number is 13911392101301011 and regardless of using sprintf or number_format i get the same strange result.
sprintf('%017.0f', "13911392101301011"); // Result is 13911392101301012
number_format(13911392101301011, 0, '', ''); // Result is 13911392101301012
sprintf('%017.0f', "13911392101301013"); // Result is 13911392101301012
number_format(13911392101301013, 0, '', ''); // Result is 13911392101301012
As you actually have the number as a string, use the %s modifier:
sprintf('%s', "13911392101301011"); // 13911392101301011
Note that PHP is using a signed integer internally. The size depends on your system.
32bit system:
2^(32-1) = 2147483648
64bit system:
2^(64-1) = 9223372036854775808
-1 because 1 bit is reserved for the signage flag.
Since you are dealing with large numbers here, you may want to keep them as strings and perform numerical operation on the string values using BCMath functions.
$val = "13911392101301011";
echo $val; // 13911392101301011
echo bcadd($val, '4'); // 13911392101301015
echo bcmul($val, '2'); // 27822784202602022
You can do easily this way :-
ini_set("precision",25); // change 25 to whatever number you want or need
$num = 13911392101301011;
print $num;
Documentation states that $number in number_format is float so there is explicit typecast. Equivalent would look like this:
sprintf('%017.0f', (float) "13911392101301011");
Float is precise to around 14 digits and your number has 17 digits.
Your number_format call is setting the . and , to blank
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
try this:
number_format(13911392101301011, 0, '.', ',');
I have this php
<?php echo round($price, 2); ?>
and the $price maybe 1.0000
i want 1.00 but i get only 1
any ideas
number_format works:
echo number_format($price, 2);
The following printf() call should work for you:
<?php printf("%.2f", $price); ?>
The documentation for this syntax is best described on the sprintf() page.
number_format is your best bet.
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
Example:
<?php echo number_format(1.0000, 2, '.', ','); ?>