$val = 95454545.455
How to round $val so the result will be 95454546?
Because using round() only return 95454545
EDIT
ceil() is almost correct, but if $val = 95454545.444, I want the function return to 95454545
Regards,
Elmer
Try this
ceil — Round fractions up, Returns the next highest integer value by rounding up value if necessary.
<?php
$val = 95454545.455;
$result = ceil($val);
echo 'Result '.$result;
?>
Use ceil() instead which will round to the next big value,
echo ceil($val);
UPDATE
You can acheive more precision with round() and HALF UP flag,
echo round(95454545.544, 0, PHP_ROUND_HALF_UP); //95454546
echo round(95454545.44, 0, PHP_ROUND_HALF_UP); //95454545
Try this number_format it will round up decimal numbers
<?php
$val = 95454545.455;
$result = number_format($val , 3, '.', ',');
echo 'Result '.$result;
<?
I think, I've found the formula for this kind of problem.
ex:
$var = 95454545.454
I only have to add 0.055 value in it.
$new_var = $var + 0.055 -> $new_var = 95454545.509
$result = round($new_var) -> $result = 95454546
Related
I've tried casting to float and number_format but float will always round at two and number_format is fixed on the amount of decimals you specify.
So how can I do this like the following conversion
11.2200 -> 11.22
11.2000 -> 11.20
11.2340 -> 11.234
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
This might help, You can use sprintf given by PHP.
You can use float casting
echo (float) 11.2200;
echo "<br/>";
echo (float) 11.2000;
echo "<br/>";
echo (float) 11.2340;
and you have to check number of digits after decimal point and than get value like below :
$val=(float) 11.2000;
if(strlen(substr(strrchr($val, "."), 1))<2){
echo number_format($val,2);
}
You may use the round() function for this.
i-e round(number,precision,mode);
Example:
echo(round(11.2200,2));
Output
11.22
Thanks
Not sure if you need a fix for this anymore, but I just ran into the same problem and here's my solution:
$array = array(11.2200, 11.2000, 11.2340);
foreach($array as $x)
{
// CAST THE PRICE TO A FLOAT TO GET RID OF THE TRAILING ZEROS
$x = (float)$x
// EXPLODE THE PRICE ON THE DECIMAL (IF IT EXISTS)
$pieces = explode('.',$x);
// IF A SECOND PIECE EXISTS, THAT MEANS THE FLOAT HAS AT LEAST ONE DECIMAL PLACE
if(isset($pieces[1]))
{
// IF THE SECOND PIECE ONLY HAS ONE DIGIT, ADD A TRAILING ZERO TO FORMAT THE CURRENCY
if(strlen($pieces[1]) == 1)
{
$x .= '0';
}
}
// IF NO SECOND PIECE EXISTS, ADD A .00 TO IT TO FORMAT THE CURRENCY VALUE
else
{
$x .= '.00';
}
}
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!
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
I have a basic index.php page with some variables that I want to print in several places - here are the variables:
<?php
$firstprice = 1.50;
$secondprice = 3.50;
$thirdprice = 20;
?>
My challenge is that later in the document, when I print, I get the prices without the second '0' in the price - this is what happens:
<?php print "$firstprice";?> // returns 1.5 - not 1.50!
SO - I know how to do this with JS, but how is this done in PHP 5+? Basically I want to print the second '0' if there is already a decimal, so if the variable is equal to '3', it stays as '3', but if it's equal to '3.5' it converts to display '3.50' with a second '0', etc.
Here's a JS example - what's the PHP equivalent?
JS:
.toFixed(2).replace(/[.,]00$/, ""))
Many thanks!!
This is simple and it will also let you tweak the format to taste:
$var = sprintf($var == intval($var) ? "%d" : "%.2f", $var);
It will format the variable as an integer (%d) if it has no decimals, and with exactly two decimal digits (%.2f) if it has a decimal part.
See it in action.
Update: As Archimedix points out, this will result in displaying 3.00 if the input value is in the range (2.995, 3.005). Here's an improved check that fixes this:
$var = sprintf(round($var, 2) == intval($var) ? "%d" : "%.2f", $var);
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
more info here
http://php.net/manual/en/function.number-format.php
You could use
if (is_float($var))
{
echo number_format($var,2,'.','');
}
else
{
echo $var;
}
What about something like this :
$value = 15.2; // The value you want to print
$has_decimal = $value != intval($value);
if ($has_decimal) {
echo number_format($value, 2);
}
else {
echo $value;
}
Notes :
You can use number_format() to format value to two decimals
And if the value is an integer, just display it.
you can use number_format():
echo number_format($firstprice, 2, ',', '.');
Alternatively way to print
$number = sprintf('%0.2f', $numbers);
// 520.89898989 -> 520.89
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));