php number format value 999999 but output show 10000000 - 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.

Related

Number formatting using php

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

replace integer into exact 2 decimal after '.', for example 2 into 2.00

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

Printing big numbers in PHP

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, '.', ',');

how do i round in php with keeping the two zeros

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, '.', ','); ?>

PHP number format

I have this string :
000000000000100
and need to convert it to:
1,00
So, the rules are:
Divide the number by 100 and use a comma as decimal separator
Strip leading zeros
Keep two decimals
From the PHP Manual page on number_format:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
If you want numbers like 123456 be formatted as 1234,45, use:
echo number_format($number / 100, 2, ",", "");
If you need a dot as thousands separator (1.234,56):
echo number_format($number / 100, 2, ",", ".");
The zeros are automatically removed by PHP when converting the string to a number.
string number_format ( float $number ,
int $decimals = 0 ,
string $dec_point = '.' ,
string $thousands_sep = ',' )
Manual: http://php.net/manual/en/function.number-format.php
// divide by 100 to shift ones place.
echo number_format((int)'000000000000100' / 100,2,',','');

Categories