Excel rounddown function equivalent in php - php

Everyday we need to calculate number of units allocated based on two variables called investment amount and NAV. This we are doing now in Excel using rounddown function, which I am trying to achieve in PHP but it is sometimes rounding up the result.
In Excel:
Cell A1 = 287011570.56
Cell B1 = 14.9482
Cell C1 = =ROUNDDOWN(A1/ROUND(B1,4),4)
= 19200410.1202
In PHP:
<?php
$var1=287011570.56;
$var2=14.9482;
$var3=$var1/$var2;
$var4 = floor(($var3) * 100000 + .5) * .00001;
$var5 = intval($var4);
$var6 = strlen($var5)+5;
$test = substr("$var4", 0, $var6);
print "$test";
?>
Result : 19200410.1203
The result should contain 4 digits after decimal and it should not round up the result.
Thanks in advance.

You shouldn't need to add the 0.5 when you are using floor(). That can move the value to the next highest value which you don't want when you are doing division.
From your example just doing:
floor($var3 * 10000) / 10000
Gave me the correct result and should work properly.

Use Round - float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ).
You need to pass PHP_ROUND_HALF_DOWN as 3rd parameter(Mode).
round($value, 4, PHP_ROUND_HALF_DOWN);
If mode parameter is not available in your version of PHP(as it didn't work for me) then use this:
floor($value * 10000) / 10000;

Related

Best way to convert a decimal number to next non-decimal number in PHP

Example: if a number is: 3.2 then it will become 4
round($variable), function only do this, if the decimal value is 0.6 or higher.
Is their any function in PHP to do so? Currently, i am using this code to do what i want:
if ($totalpages > round($totalpages)) // If the number is less than 0.06
{
$round1 = round($totalpages);
$round2 = $totalpages-round($totalpages);
$round3 = 1-$round2;
$round4 = $totalpages+$round3;
$totalpages = $round4; //
}
else
$totalpages = round($totalpages);
Is their any better way to do so?
Your looking for ceil()
ceil — Round fractions up
Description
float ceil ( float $value ) Returns the next highest integer value by
rounding up value if necessary.
http://php.net/manual/en/function.ceil.php

round up decimal values to exact integer value php

I have came across the php rounding decimal values by using:
The round() function rounds a floating-point number.
To round a number UP to the nearest integer, look at the ceil() function.
To round a number DOWN to the nearest integer, look at the floor() function.
I have an example of dividing 1000/13 which when we use
round(1000/13, 2) will give you 999.98 and then i will use ceil to round it to 1000.
Now, my value is 925.6 which has to be divided by 11 and I tried with the round(925.6/11, 2) which will give 84.15, then if u multiply this with 11(84.15 * 11)then you will get 925.56. Now how can achieve the exact decimal if any decimal input.
Rounding-off a Double or Float using round(), ceil() or floor() all have one thing in Common and that is: by using them you are subscribing to losing the Precision of your Data. This means; once the die is cast, you cannot get the Original value again.... it is that simple:
If you want to retain both the Truncated and the Original Values, you have to do it differently.... You may need to keep 2 Variables or simply put your Variables in an Array. A Simple example would demonstrate this even better:
<?php
$num = (925.6/11);
// STORE THE ORIGINAL & ROUNDED VALUES IN AN ARRAY (IN CASE YOU NEED BOTH)
$arrNumbers = [
'original' => $num,
'rounded' => round($num, 2),
];
// NOW; FOR YOUR MULTIPLICATIONS:
$original = $arrNumbers['original'] * 11;
$broken = $arrNumbers['rounded'] * 11;
var_dump($original); //<== float 925.6
var_dump($broken); //<== float 925.65 :: ADDED AN EXTRA 0.05 (BOGUS, HUH?)
var_dump($arrNumbers); //<== array (size=2) 'original' => float 84.1454545455 'rounded' => float 84.15
UPDATE: NUMERATOR AS A VALUE FROM FORM-FIELD.
<?php
// ASSUME THE VALUE OF THE NUMERATOR IS: 925.6
$numerator = htmlspecialchars(trim($_POST['some_field']));
// AND THE DENOMINATOR IS NOW SOMETHING LIKE MONTH 11 AS IN: 11.0000
$denominator = 11.0000;
// NO PROBLEMS STILL...
$num = ($numerator/$denominator);
// STORE THE ORIGINAL & ROUNDED VALUES IN AN ARRAY (IN CASE YOU NEED BOTH)
$arrNumbers = [
'original' => $num,
'rounded' => round($num, 2),
];
// NOW; FOR YOUR MULTIPLICATION,
// TO GET THE STARTING VALUES BACK DO:
$original = $arrNumbers['original'] * 11;
$broken = $arrNumbers['rounded'] * 11;
var_dump($original); //<== float 925.6
var_dump($broken); //<== float 925.65 :: ADDED AN EXTRA 0.05 (BOGUS, HUH?)
var_dump($arrNumbers); //<== array (size=2) 'original' => float 84.1454545455 'rounded' => float 84.15

PHP round to integer

I want to round a number and I need a proper integer because I want to use it as an array key. The first "solution" that comes to mind is:
$key = (int)round($number)
However, I am unsure if this will always work. As far as I know (int) just truncates any decimals and since round($number) returns a float with theoretically limited precision, is it possible that round($number) returns something like 7.999999... and then $key is 7 instead of 8?
If this problem actually exists (I don't know how to test for it), how can it be solved? Maybe:
$key = (int)(round($number) + 0.0000000000000000001) // number of zeros chosen arbitrarily
Is there a better solution than this?
To round floats properly, you can use:
ceil($number): round up
round($number, 0): round to the nearest integer
floor($number): round down
Those functions return float, but from Niet the Dark Absol comment: "Integers stored within floats are always accurate, up to around 2^51, which is much more than can be stored in an int anyway."
round(), without a precision set always rounds to the nearest whole number. By default, round rounds to zero decimal places.
So:
$int = 8.998988776636;
round($int) //Will always be 9
$int = 8.344473773737377474;
round($int) //will always be 8
So, if your goal is to use this as a key for an array, this should be fine.
You can, of course, use modes and precision to specify exactly how you want round() to behave. See this.
UPDATE
You might actually be more interested in intval:
echo intval(round(4.7)); //returns int 5
echo intval(round(4.3)); // returns int 4
What about simply adding 1/2 before casting to an int?
eg:
$int = (int) ($float + 0.5);
This should give a predictable result.
Integers stored within floats are always accurate, up to around 253, which is much more than can be stored in an int anyway. I am worrying over nothing.
For My Case, I have to make whole number by float or decimal type
number. By these way i solved my problem. Hope It works For You.
$value1 = "46.2";
$value2 = "46.8";
// If we print by round()
echo round( $value1 ); //return float 46.0
echo round( $value2 ); //return float 47.0
// To Get the integer value
echo intval(round( $value1 )); // return int 46
echo intval(round( $value2 )); // return int 47
My solution:
function money_round(float $val, int $precision = 0): float|int
{
$pow = pow(10, $precision);
$result = (float)(intval((string)($val * $pow)) / $pow);
if (str_contains((string)$result, '.')) {
return (float)(intval((string)($val * $pow)) / $pow);
}
else {
return (int)(intval((string)($val * $pow)) / $pow);
}
}
Round to the nearest integer
$key = round($number, 0);

How to get first 4 digits from an integer in PHP

After a few calculations I get:
$int = 14.285714285714;
How can I take only the first four digits? Expected output: 14.28
Doing this with string functions is absolutely the wrong way to go about this, and these nearly identical answers look pretty spammy. If you want to round a number, round a number!
$int = round(14.285714285714, 2);
To truncate (as opposed to rounding), floor is the correct function in PHP:
$int = floor(14.285714285714 * 100) / 100;
Both work without any type conversions or casting.
Also note that a number with decimal places is categorically not an integer.
use substr function
$int = 14.285714285714;
echo substr($int, 0, 5);
$newint = (float)substr($int, 0, 5);
IF you want to round the number you can use
round($int, 2);
OUTPUT WILL BE : 14.29
LINK HOW TO ROUND
Try number format,
string number_format ( float $number [, int $decimals = 0 ] )
so
$int = 14.285714285714;
$small = (float)number_format ( $int ,2,'.', '' ); //gets rid of the "," for thousand separator
http://us2.php.net/manual/en/function.number-format.php
floor(100 * 14.285714285714) / 100
14.28

How to convert float percent value to integer percent?

How to convert float percent number value to integer percent number value in PHP?
Example: 3.8461538461538 % = 4 %
Update:
Here is my code , I coded to get percent value, but I get decimal value, but I need it without decimal value.
$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo $percent ;
echo "%";
This is done with round() - Rounds to closest int
<?php
echo round('3.8461538461538'); //4
?>
There is also:
floor() -- Round fractions down.
ceil() -- Round fractions up.
use round() . it's a PHP function.
Edit:
Given Code:
$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo round($percent). "%";
For eg:
$number="3.8461538461538%"; //if it's a string with % in the end.
$number=substr($number, 0,-1); //It type converts automatically here. Lazy code :)
$number_rounded=round($number);
or
$number=3.8461538461538; //If it's just a float.
$number_rounded=round($number);
<?php number_format(3.8461538461538); ?>
You can specify in second argument the number of decimal points (default is 0).

Categories