number_format rounding up number that end with 5 decimal - php

Is there a way to make number_format not to round up numbers that end up with a "5" decimal?
echo number_format(25.58,1); // 25.6 - OK
echo number_format(85.72,1); // 85.7 - OK
echo number_format(26.25,1); // 26.3 - WHY? 26.2 needed here.
Note: I'm not asking this

Instead of using number_format() use the round function. You can specify how you want it to round.
echo round(25.58, 1, PHP_ROUND_HALF_DOWN); // 25.6
echo round(85.72, 1, PHP_ROUND_HALF_DOWN); // 85.7
echo round(26.25, 1, PHP_ROUND_HALF_DOWN); // 26.2

It's unclear on what you are trying to achieve. Based on your comment of 'ignoring' numbers that end in five i have mocked up a very basic quick function that does this
echo format_numbers(25.58,1); // 25.6
echo format_numbers(85.72,1); // 85.7
echo format_numbers(26.25,1); // 26.2
function format_numbers($number, $decimal)
{
if(substr($number, -1) == '5'){
$number = $number - 0.1;
}
return number_format($number, $decimal);
}
This example is for PHP versions that do not support the prefered method of round(26.25, 1, PHP_ROUND_HALF_DOWN). The above example will deduct 0.1 from the number thus forcing number_format() to round down.

Related

Use round or floor depending the number after the comma

What would be the solution to have:
7.1 => 7
7.5 => 7
7.8 => 8
So I need to round number or floor depending on the number after the comma.
How to do that?
Thanks.
You should be able to use the constant, PHP_ROUND_HALF_DOWN, to have the round function round down when it is half way.
echo round(7.1, 0, PHP_ROUND_HALF_DOWN) . "\n";
echo round(7.5, 0, PHP_ROUND_HALF_DOWN) . "\n";
echo round(7.8, 0, PHP_ROUND_HALF_DOWN) . "\n";
Output:
7
7
8
From the manual:
Round val down to precision decimal places towards zero, when it is half way there. Making 1.5 into 1 and -1.5 into -1.
PHP Demo: https://eval.in/427706
One way to do this is to split the value at the decimal (or comma? your example is using decimals) and test the trailing number to see whether you want to use floor or ceiling.
$test = 7.6
$arrayTest = explode(".",$test);
if(isset($arrayTest[1]) && $arrayTest[1] > 5) {
//do something
} else {
//do something else
}

PHP - round() function with two decimals even whole numbers

How can I have two decimals values even with the whole number using round() function of php
SAMPLE
$number = round(123.456,2); // if i use number format it will not round the decimals so it will print 123.45 not 123.46
$number2 = round(123,2);
echo $number; // => 123.46
echo $number2; // => I want this one to print 123.00
$number2 = number_format(123, 2, '.', '');
Rounding a whole number wont return 2 decimal places. So use number_format(); to make it display them.

How can I round up a number without php function ceil

I'm trying to build a function that output will be rounded up number. I know there is a php function, but I want to make this function for another purpose.
you want ceil without using ceiling...
intval($number + .5)
this is the same thing, but you are still using a built in function.
EDIT: apparently the above solution does not work as I intended it to in PHP. You can use the round function to similar effect
round($number + .5)
or something similar to another answer:
$n = intval($number + .5);
if($n < $number){
$n++;
}
May this do it?
function newceil($num)
{
$re=intval($num);
if($re<$num) $re++;
return $re
}
You could cut off the fractional part by casting it to an integer and afterwards check, whether the so derived value is smaller or even the initial value.
$input = 3.141592653;
$intVersion = (int) $input;
if($intVersion<$input) $intVersion++;
return $intVersion
If you want to round up/down
You can use round method
/* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_UP); // 1.6
echo round( 1.54, 1, PHP_ROUND_HALF_UP); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_UP); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_UP); // -1.5
/* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round( 1.54, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_DOWN); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_DOWN); // -1.5
/* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_EVEN); // 1.6
echo round( 1.54, 1, PHP_ROUND_HALF_EVEN); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_EVEN); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_EVEN); // -1.5
/* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_ODD); // 1.5
echo round( 1.54, 1, PHP_ROUND_HALF_ODD); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_ODD); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_ODD); // -1.5
?>
Note: ceil round up
You can use round() and floor() and number_format() for round up number.
echo round(153.751); // 154
echo floor(153.751); // 153
echo number_format(153.751); // 154

How can I make sure a float will always be rounded up with PHP?

I want to make sure a float in PHP is rounded up if any decimal is present, without worrying about mathematical rounding rules. This function would work as follows:
1.1 to 2
1.2 to 2
1.9 to 2
2.3 to 3
2.8 to 3
I know the round() function exists but I don't see any function for rounding up if any decimal is found. Is there any easy way to do this?
Use the ceil function:
$number = ceil(1.1); //2
I know this is an old topic, however it appears in Google. I will extend Blake Plumb's answer regarding precision.
ceil(1024.321 * 100) / 100;
Multiplying by 100 and dividing by 100 only works with one-hundredths. This isn't accurate on tenths, one-thousandths, one-hundred thousandths, etc.
function round_up($number, $precision = 2)
{
$fig = pow(10, $precision);
return (ceil($number * $fig) / $fig);
}
Results:
var_dump(round_up(1024.654321, 0)); // Output: float(1025)
var_dump(round_up(1024.654321, 1)); // Output: float(1024.7)
var_dump(round_up(1024.654321, 2)); // Output: float(1024.66)
var_dump(round_up(1024.654321, 3)); // Output: float(1024.655)
var_dump(round_up(1024.654321, 4)); // Output: float(1024.6544)
var_dump(round_up(1024.654321, 5)); // Output: float(1024.65433)
var_dump(round_up(1024.654321, 6)); // Output: float(1024.654321)
Notes:
Thanks for the contributions from Joseph McDermott and brandom for improving my original snippet.
The official Ceil function will do that for you.
Taken from the example:
<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
echo ceil(-3.14); // -3
?>
I know this question has long since been answered, but it came up when I did a google search on the topic. If you want to round up with precision, then a good method would be to use the ceil function and times the number by how many decimal points you want to represent and then divide by that number.
ceil(1024.321*100)/100
would produce 1024.33
I like Ash's response, although I would have:
$fig = (int) str_pad('1', $precision + 1, '0');
Makes sense that if I provide precision '2', I would expect it rounded to 2 decimal places. Matter of choice though I suppose. Thanks for the answer Ash, works well.

how to create "pretty" numbers?

my question is: is there a good (common) algorithm to create numbers, which match well looking user understood numbers out of incomming (kind of random looking for a user) numbers.
i.e. you have an interval from
130'777.12 - 542'441.17.
But for the user you want to display something more ...say userfriendly, like:
130'000 - 550'000.
how can you do this for several dimensions?
an other example would be:
23.07 - 103.50 to 20 - 150
do you understand what i mean?
i should give some criteria as well:
the interval min and max should
include the given limits.
the "rounding" should be in a
granularity which reflects the
distance between min and max (meaning
in our second example 20 - 200
would be too coarse)
very much honor you'll earn if you know a native php function which can do this :-)
*update - 2011-02-21 *
I like the answer from #Ivan and so accepted it. Here is my solution so far:
maybe you can do it better. i am open for any proposals ;-).
/**
* formats a given float number to a well readable number for human beings
* #author helle + ivan + greg
* #param float $number
* #param boolean $min regulates wheter its the min or max of an interval
* #return integer
*/
function pretty_number($number, $min){
$orig = $number;
$digit_count = floor(log($number,10))+1; //capture count of digits in number (ignoring decimals)
switch($digit_count){
case 0: $number = 0; break;
case 1:
case 2: $number = round($number/10) * 10; break;
default: $number = round($number, (-1*($digit_count -2 )) ); break;
}
//be sure to include the interval borders
if($min == true && $number > $orig){
return pretty_number($orig - pow(10, $digit_count-2)/2, true);
}
if($min == false && $number < $orig){
return pretty_number($orig + pow(10, $digit_count-2)/2, false);
}
return $number;
}
I would use Log10 to find how "long" the number is and then round it up or down. Here's a quick and dirty example.
echo prettyFloor(23.07);//20
echo " - ";
echo prettyCeil(103.50);//110
echo prettyFloor(130777.12);//130000
echo " - ";
echo prettyCeil(542441.17);//550000
function prettyFloor($n)
{
$l = floor(log(abs($n),10))-1; // $l = how many digits we will have to nullify :)
if ($l<=0)
$l++;
if ($l>0)
$n=$n/(pow(10,$l)); //moving decimal point $l positions to the left eg(if $l=2 1234 => 12.34 )
$n=floor($n);
if ($l>0)
$n=$n*(pow(10,$l)); //moving decimal point $l positions to the right eg(if $l=2 12.3 => 1230 )
return $n;
}
function prettyCeil($n)
{
$l = floor(log(abs($n),10))-1;
if ($l<=0)
$l++;
if ($l>0)
$n=$n/(pow(10,$l));
$n=ceil($n);
if ($l>0)
$n=$n*(pow(10,$l));
return $n;
}
This example unfortunately will not convert 130 to 150. As both 130 and 150 have the same precision. Even thou for us, humans 150 looks a bit "rounder". In order to achieve such result I would recommend to use quinary system instead of decimal.
You can use php's round function which takes a parameter to specify the precision.
<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>
The number_format() function handles "prettifying" numbers with arbitrary thousands/decimal characters and decimal places, but you'd have to split your ranges/strings into individual numbers, as number_formation only works on one number at a time.
The rounding portion would have to handled seperately as well.
I haven't seen ready algorithm or function for that. But it should be simple, based on string replacement (str_replace, preg_replace), number_format and round functions.
This actually is kind of a special case, that can be addressed with the following function:
function roundto($val, $toceil=false) {
$precision=2; // try 1, 2, 5, 10
$pow = floor(log($val, 10));
$mult = pow(10, $pow);
$a = $val/$mult*$precision;
if (!$toceil) $a-=0.5; else $a+=0.5;
return round($a)/$precision*$mult;
}
$v0=130777.12; $v1=542441.17;
echo number_format(roundto($v0, false), 0, '.', "'").' - '
.number_format(roundto($v1, true), 0, '.', "'").'<br/>';
$v0=23.07; $v1=103.50;
echo number_format(roundto($v0, false), 0, '.', "'").' - '
.number_format(roundto($v1, true), 0, '.', "'").'<br/>';
Outputs exactly this:
100'000 - 550'000
20 - 150
For any other case of number formatting it might be interesting to have a look at my newly published PHP class "php-beautiful-numbers", which I use in almost ever project to display run times ("98.4 µs" [= 9.8437291615846E-5]) or numbers in running text (e.g. "you booked two flights." [= 2]).
https://github.com/SirDagen/php-beautiful-numbers

Categories