printing with the penny value in php - php

I have these value stored in a decimal 10,2 field
1052730
956700
How do i print this using php so that the value is like
$10,527.30
$9,567.00
basically i am trying to avoid the value as
$1,052,730 <--- this i dont want

You can use the
money_format($format, $value)
function in php. The details of the formatting is given here.

Well, assuming that 1052730 is really 10527.30 as alluded to in your question:
$number = 1052730;
$decimals = $number % 100; //30 in this case
$digits = floor($number / 100);
$paddedDecimals = str_pad($digits, 2, '0', STR_PAD_LEFT);
$out = '$' . number_format($digits, 0).'.'.$paddedDecimals;
echo $out; // $10,527.30
There are no floating point calculations used for the decimal part, so there's no need to worry about precision issues (although at this precision it would likely be hard to get a float error in there)...

Just divide by 100:
<?php
echo number_format(1052730/100, 2, '.', ',') . PHP_EOL;
echo number_format(956700/100, 2, '.', ',') . PHP_EOL;

printf ("$%01.2f", ($input / 100));

Related

Unexpected result for number_format and sprintf when trying to display number with two decimals

I have a calculation that returns this double: 6.4971508379888. If I cast it to a float and echo it, it's the same number. I want to display (not round) the number with only two decimals, so I tried:
number_format((float)$number, 2, '.', '')
and
sprintf('%0.2f', (float)$number)
but in both cases I see 6.50 instead of 6.49. Why is this happening?
PHP automatically round the value based on given precision
If you want your expected results then follow the following code.
$precision = 2;
$number = floor($number * pow(10,$precision))/pow(10,$precision);
echo number_format((float)$number, $precision, '.', '');
This is a slight modification to the accepted answer because the solution provided wasn't giving an accurate result. It was causing the last digit to get rounded (down).
$digits = 2;
$number = floor($number * pow(10, ($digits + 1)) / pow(10, ($digits + 1));
echo number_format($number, $digits, '.', '');
Also, there should be no need to type-cast to a float when passing the number to number_format(). The calculation (and the function itself) will automatically set the type accordingly.

Delete digits after two decimal point not round number using PHP

My number: 53.199999999999996
I have tried all of these:
sprintf("%01.2f", $number); // 53.20
sprintf('%0.2f', $number); // 53.20
floor(($number * 100)) / 100; // 53.2
intval(($number * 100)) / 100; // 53.2
I want: 53.19
How can I do that?
You can use number_format(): http://php.net/number_format
$number = 53.1999999
echo int() number_format((float)$number, 2, '.', '');
//53.19
You need floor() in this way:
echo floor($number*100)/100;
Or you cast to integer:
echo 0.01 * (int)($number*100);
This way it will not be rounding up.
I have checked and using intval its works on php version 5.3, however you have mentioned its not worked at your end.
$number = 53.1999999;
echo intval(($number*100))/100;
you must set precision (floating point) before set this float number
ini_set('precision', 17);
$number = 53.199999999999996;
$number = ($pos = strpos($number,'.')) ? substr($number,0,$pos + 3) : number_format($number);
echo $number ;
output :
53.19
Link
PHP round float numbers with this setting, if you set variable without quoting that like :
$number = 53.199999999999996;
echo $number;
PHP will round this number
after var_dump , print ,echo , ... output will be :
53.2
our you can set number value with quoting
$number = '53.199999999999996';
echo $number;
PHP do not round this because it's string now!

Set precision for a float number in PHP

I get a number from database and this number might be either float or int.
I need to set the decimal precision of the number to 3, which makes the number not longer than (regarding decimals) 5.020 or 1518845.756.
Using PHP
round($number, $precision)
I see a problem:
It rounds the number. I need a function to only cut the decimals short, without changing their values which round( ) seems not to follow.
You can use number_format() to achieve this:
echo number_format((float) $number, $precision, '.', '');
This would convert 1518845.756789 to 1518845.757.
But if you just want to cut off the number of decimal places short to 3, and not round, then you can do the following:
$number = intval($number * ($p = pow(10, $precision))) / $p;
It may look intimidating at first, but the concept is really simple. You have a number, you multiply it by 103 (it becomes 1518845756.789), cast it to an integer so everything after the 3 decimal places is removed (becomes 1518845756), and then divide the result by 103 (becomes 1518845.756).
Demo
Its sound like floor with decimals. So you can try something like
floor($number*1000)/1000
If I understand correctly, you would not want rounding to occur and you would want the precision to be 3.
So the idea is to use number_format() for a precision of 4 and then remove the last digit:
$number = '1518845.756789';
$precision = 3;
echo substr(number_format($number, $precision+1, '.', ''), 0, -1);
Will display:
1518845.756
rather than:
1518845.757
Links : number_format() , substr()
See this answer for more details.
function numberPrecision($number, $decimals = 0)
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = pow(10, $decimals);
return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}
$num=5.1239;
$testnum=intval($num*1000)/1000;
echo $testnum; //return 5.123

Printing PHP float with 2 digits after the decimal point?

Is there an easy way to echo a float number with a specific amount of digits after the decimal point?
For example: $sum = 3.1234566768; I would like to echo $sum and get: 3.12.
use number_format()
number_format($sum,2);
Try with:
$sum = 3.1234566768;
$rounded = round($sum, 2);
echo number_format($sum, 2); // 3.12
echo number_format((float)$ans, 4, '.', '');
I think this will work out

PHP number_format is rounding?

I have a price "0,10" or "00000,10"
Now when i try
number_format($price, 2, ',', '')
I get 0,00.
How can i fix this? I want 0,10 $.
I don't want rounding.
Or when i have 5,678, i get 5,68. But i want 5,67.
Several people have mentioned rounding it to 3 and then dropping the last character. This actually does not work. Say you have 2.9999 and round it to 3 it's 3.000.
This is still not accurate, the best solution is this:
$price = '5.678';
$dec = 2;
$price = number_format(floor($price*pow(10,$dec))/pow(10,$dec),$dec);
What this does is takes the price and multiplies it by 100 (10^decimal) which gives 567.8, then we use floor to get it to 567, and then we divide it back by 100 to get 5.67
You can increase the size of the number before rounding down with floor:
$price = floor($price * 100) / 100;
$formatted = number_format($price, 2, ',', '');
Another solution, which may give better precision since it avoids floating-point arithmetic, is to format it with three decimals and throw away the last digit after formatting:
$formatted = substr(number_format($price, 3, ',', ''), 0, -1);
you should convert comma-filled number back to normal decimal before with str_replace.
$number = str_replace(",", ".", $number);
and then you can use number_format
"00000,10" is a string. You should a decimal point. To get the desired behaviour, you could use:
echo substr(number_format(str_replace(',', '.', $price), 3, ',', ''), 0, -1);
Use this (needs activated intl PHP extension)
$numberFmtCurrency = new NumberFormatter('de_AT', NumberFormatter::CURRENCY);
$numberFmtCurrency->setAttribute(NumberFormatter::ROUNDING_INCREMENT, 0);
$numberFmtCurrency->formatCurrency(328.13, 'EUR'); // prints € 328.13 (and not 328.15)
If you are literally just wanting to clear leading zeroes and just limit the length, rather than round to a certain amount of decimal places, a more generalised solution could be this function:
function cutafter($string,$cutpoint,$length)
{
$temp = explode($cutpoint,$string);
$int = $temp[0];
$sub = $temp[1];
return number_format($int,0).','.substr($sub,0,$length);
}
Example:
$number = "005,678";
$answer = cutafter($number,",",2);
$answer now equals "5,67"
Just before number_format is executed the string "0,10" is converted by php to an number. because php always uses the engish notation the it won't look after the comma.
echo "4 apples" + 2;
output: 6
The " apples" part is ignored just as your ",10" is ignored.
Converting the "," to a "." allows php to see the other digits.
$price = str_replace(',', '.', '0,10');
number_format($price, 2, ',', '');
My problem was that html validator error messege thar number_format() argument is not double.
I solved this error message by placing floatval for that argument like number_format(floatval($var),2,'.',' ') and that is working good.
function format_numeric($value) {
if (is_numeric($value)) { // is number
if (strstr($value, ".")) { // is decimal
$tmp = explode(".", $value);
$int = empty($tmp[0]) ? '0' : $tmp[0];
$dec = $tmp[1];
$value = number_format($int, 0) . "." . $dec;
return $value;
}
$value = number_format($value);
return $value;
}
return $value; // is string
}
Unit Testing:
Passed / 1100000 => 1,100,000
Passed / ".9987" => .9987
Passed / 1100.22 => 1,100.22
Passed / 0.9987 => 0.9987
Passed / .9987 => 0.9987
Passed / 11 => 11
Passed / 11.1 => 11.1
Passed / 11.1111 => 11.1111
Passed / "abc" => "abc"
See this answer for more details.
function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = pow(10, $decimals);
$number = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
return number_format($number, $decimals, $decPoint, $thousandsSep);
}

Categories