how do i round in php with keeping the two zeros - php

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

Related

php number format value 999999 but output show 10000000

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.

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

Padding zeroes to Price

$price = 10.00;
list($dollars, $cents) = explode('.', $price);
echo $dollars . '.' . $cents;
... almost works except that the zeros are omitted. 10.00 becomes 10 and 10.10 becomes 10.1
I see there's a padding function for strings, but anything for numbers or floats?
How do I fix this?
You can use number_format:
echo number_format($price, 2); // Would print 10.00
You can specify a separator for the decimal point and another one for the thousands:
echo number_format(1234.56, 2, ',', ' '); // Would print 1 234,56
Use Sprintf
$digit = sprintf("%02d", $digit);
For more information, refer to the documentation of sprintf.
number_format is what you want: http://php.net/manual/en/function.number-format.php
Though i would recommend number_format, you could use
sprintf('%02.2f', $price)
if you want to rely on string functions.

How to add .00 in any value using PHP?

I want to add .00 to my value.
For example:
100 will be 100.00
100.26 will be 100.26 only.
$YOUR_VALUE = 1000.25;
echo number_format($YOUR_VALUE, 2);
number_format() can be your friend
Like #Gaurav said, use the number_format() function. Simply pass it the value and the number of digits you want there to be after the decimal point:
$value = 100;
echo number_format($value, 2); //prints "100.00"
Note that by default, it will also insert commas as the thousands separator:
$value = 2013;
echo number_format($value, 2); //prints "2,013.00"
You can change the characters that are used as the decimal point and thousands separator by passing them in as the third and fourth parameters to the function:
$value = 2013;
echo number_format($value, 2, ',', ' '); //prints "2 013,00"
number_format(100, 2, '.', ' ')
you can use
round()
or
number_format()

Categories