php number_format display the decimal - php

I have a number like this 18914439.4524345860
and I want to display that number like this 18.91
I have try the number_format function
$x = 18914439.4524345860;
echo number_format($x, 2, ".", ".");
and the result is
18.914.439.45
how to show the number like this 18.91?
Thanks for your attention...

hope this help you :
$int = 18914439.4524345860/1000000;
echo round($int,2);
/* Output 18.91*/

18914439.4524345860 is a complete different number than 18.91, not just another representation.
You could divide by 1000000 first, and then use number_format:
$x = 18914439.4524345860;
echo number_format($x / 1000000, 2, ".", ".");

Related

Round 2 decimals

I have an issue with php 5.6 rounding to two decimals. If you test the following it will export $y exactly as 19.620000000000001
I know there are solutions to display it using number_format, but how do I make it to be exactly 19.62?
Thanks.
$x = 19.620000000000001;
$y = round($x, 2);
var_export($y);
Later Edit.
I need it for exact math operations, not display.
Try using echo to display your output. It will give you your desired output
It's because you're using var_export
Try using echo or var_dump()
$x = 19.620000000000001;
$y = round($x, 2);
echo $y;
If you do number_format:
$y = number_format($x, 2);
The $y will have 2 decimals but it will also be a string. Not sure if it's satisfactory.

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

printing with the penny value in 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));

PHP number: decimal point visible only if needed

I'd like to know if exists some function to automatically format a number by it's decimal, so if I have:
<?php
// $sql_result["col_number"] == 1,455.75
number_format ($sql_result["col_number"], 2, ".", "");
// will return 1455.75
// $sql_result["col_number"] == 1,455.00
number_format ($sql_result["col_number"], 2, ".", "");
// could I get 1455 instead of 1455.00?
?>
so my answer is if does exist some way to remove the decimals if I have DECIMAL data forma in my DB only when it's round?
Or shoud I do something like that?
<?php
// $sql_result["col_number"] == 1,455.00
str_replace(".00", "", (string)number_format ($sql_result["col_number"], 2, ".", ""));
// will return 1455
?>
floatval or simply casting to float
php > echo floatval(7.00);
7
php > echo floatval(2.30);
2.3
php > echo floatval(1.25);
1.25
php > echo floatval(1.125);
1.125
php > echo (float) 7.00;
7
php > echo (float) 2.30;
2.3
php > echo (float) 1.25;
1.25
php > echo (float) 1.125;
1.125
I actually think that your workaround is as good as any. It's simple and clear, and there's really no point talking about performance here, so just go for it.
As Emil says yours are good. But if you want to remove 0 from e.g. 7.50 too, I've got a suggestion, rtrim():
<?php
// if $sql_result["col_number"] == 1,455.50
rtrim(rtrim(number_format($sql_result["col_number"], 2, ".", ""), '0'), '.');
// will return 1455.5
?>
You could also use rtrim(), which would remove excess 0s, in the case where you might want to keep one decimal place but not the excess zeros. (For example, 4.50 becomes 4.5.) Also allows you to change the number of decimal places from 2 to any other number.
rtrim(rtrim((string)number_format($value, 2, ".", ""),"0"),".");
// 4.00 -> 4
// 4.50 -> 4.5
// 4.54000000 -> 4.54 (if you're doing more decimal places)
Actually I think the cleanest way I can think of to do this for someone that just did a search looking for this sort of thing is to do this:
( number_format ($sql_result["col_number"], 2) * 100 ) / 100;
I've been accused of doing something like this:
floatval($foo) == intval($foo) ? number_format($foo) : number_format($foo,2);
If you are targeting US currency I like to use this method:
function moneyform($number, $symbol = true) {
return str_replace(".00", "", money_format(($symbol? '%.2n' : "%!n"), $number));
}
moneyform(1300999);
-->$1,300,999
moneyform(2500.99);
-->$2,500.99
moneyform(2500.99, false);
-->2,500.99
Mine since most quantity or pieces do not require decimal, this function will only show decimal when needed.
str_replace(".00", "", number_format($this->pieces, 2));
Warren.S answer helped me out. I didn't need the number_format function, so I just did this
$value=$value-0;
But in the OP's case, he needs number_format to remove the commas. So this would work for him
$value=number_format ($sql_result["col_number"], 2, ".", "")-0;
Since I could not find a flexible solution I wrote a simple function to get the best result:
function getValueFormattedWithMinimalDecimals($value, $max_decimals = 2, $dec_point = ',', $thousands_sep = '') {
$bestNumberOfDecimals = -1;
$decimal = 0;
while ($decimal <= $max_decimals) {
$bestNumberOfDecimals = $decimal;
$valueDecimals = number_format($value, $decimal);
if (floatval($value) == $valueDecimals) {
break;
}
$decimal++;
}
if($bestNumberOfDecimals > 0 && number_format($value, $bestNumberOfDecimals) == number_format($value, 0)) {
$bestNumberOfDecimals = 0;
}
return number_format($value, $bestNumberOfDecimals, $dec_point, $thousands_sep);
}
What about
number_format($value,2) - 0;

How do I format numbers to have only two decimal places?

I want that real numbers would be for example 12.92, but not 12.9241. Is it possible to do like that?
In PHP, try number_format:
$n = 1234.5678;
// Two decimal places, using '.' for the decimal separator
// and ',' for the thousands separator.
$formatted = number_format($n, 2, '.', ',');
// 1,234.57
For PHP you can use number_format(), for MySQL use the FORMAT() function.
MySQL: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format
FORMAT(number, 2)
Example:
mysql> SELECT FORMAT(12332.123456, 4);
-> '12,332.1235
PHP: http://php.net/manual/en/function.number-format.php
$number = 1234.5678;
$formatted_number = number_format($number, 2, '.', '');
// 1234.56
$number = 1234.5678;
$teX = explode('.', $number);
if(isset($teX[1])){
$de = substr($teX[1], 0, 2);
$final = $teX[0].'.'.$de;
$final = (float) $final;
}else{
$final = $number;
}
final will be 1234.56
You can multiply your number by 100, do a rounding of the result and then divide back by 100.
Or in php use the round function round function
$result=round(12.9241, 2);

Categories