How to round up value in PHP? - php

I have a value like this:
$value = 2.3333333333;
and I want to round up this value into like this:
$value = 2.35;
I already tried round, ceil and etc but the result is not what I expected.
Please anyone help.
Thanks

Taking your question literally, this will do it:
$value = (round($original_value / 0.05, 0)) * 0.05
i.e. will round to the nearest 0.05.
If, for some reason, you want to always round up to 0.05, use
$value = (round(($original_value + 0.025) / 0.05, 0)) * 0.05

you have 3 possibility : round(), floor(), ceil()
for you :
$step = 2; // number of step 3th digit
$nbr = round(2.333333333 * $step, 1) / $step; // 2.35
$step = 4; // number of step on 3th digit
$nbr = round(2.333333333 * $step, 1) / $step; // 2.325
round
<?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
?>
floor
<?php
echo floor(4.3); // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>
ceil
<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
echo ceil(-3.14); // -3
?>

Complementary functions to round up / down to arbitrary number of decimals:
/**
* Round up to specified number of decimal places
* #param float $float The number to round up
* #param int $dec How many decimals
*/
function roundup($float, $dec = 2){
if ($dec == 0) {
if ($float < 0) {
return floor($float);
} else {
return ceil($float);
}
} else {
$d = pow(10, $dec);
if ($float < 0) {
return floor($float * $d) / $d;
} else {
return ceil($float * $d) / $d;
}
}
}
/**
* Round down to specified number of decimal places
* #param float $float The number to round down
* #param int $dec How many decimals
*/
function rounddown($float, $dec = 2){
if ($dec == 0) {
if ($float < 0) {
return ceil($float);
} else {
return floor($float);
}
} else {
$d = pow(10, $dec);
if ($float < 0) {
return ceil($float * $d) / $d;
} else {
return floor($float * $d) / $d;
}
}
}

Try:
$value = 2.3333333333;
echo number_format($value, 2);

You can use this code:
$value = 2.3333333333;
$value = round ( $value, 2, PHP_ROUND_HALF_UP);
best document is in here

Related

How to validate decimal stepping from a starting number in PHP

I need to validate that an inputted number is a valid number based on my stepping rules and round up to the nearest valid number if not. These numbers will change but one example would be:
$min = 0.25;
$step = 0.1
$qty = 0.75 // user input
so these would be valid inputs:
0.75
0.85
0.95
But these should round:
0.76 (to 0.85)
0.80 (to 0.85)
I thought I could use modulus somehow but not getting the calculation correct.
if (($qty % min) / $step == 0)) {
echo "good";
}
I've tried some variations of math that are likely very wrong
$step = 0.1;
$min = 0.25;
$qty = .85;
$h = ($qty / $min) / $step;
echo $h;
$j = mround($qty, $min-$step);
echo $j;
function mround($num, $parts) {
if ($parts <= 0) { $parts = 1; }
$res = $num * (1/$parts);
$res = round($res);
return $res /(1/$parts);
}
I think you can use fmod to do this.
$new = $original + ($step - fmod($original - $minimum, $step));
Example on 3v4l.org

Round down very large or small float by scale

I have next situation, I have some qty it can be very big or very small float like:
3698541.2569854 or 0.569875255456.
Also, I have some scale
or min step, like
0.0001 or 1000.00.
My challenge is round my qty to nearest down number by step. Example:
$qty = 323.659;
$step = 0.1;
// result must be 323.6
I have my own solution, it work well, but i have problem with wery large nums.
private function roundDownByStep($qty, $stepSize) {
$stepSize = sprintf('%.16f', $stepSize);
$stepSize = preg_replace('/|\\..+?\\K0+$/', "", $stepSize);
$precision = strlen(substr(strrchr($stepSize, "."), 1));
if ($precision == 1 && $stepSize >= 1) {
$precision = 0;
}
if ($precision == 0) {
$subArg = 1/$stepSize;
return floor(($qty * $subArg)) / $subArg;
} else {
$subArg = bcdiv('1', $stepSize, $precision);
$result = floor(bcmul($qty, $subArg, $precision));
$result = sprintf('%.16f', $result);
$result = preg_replace('/|\\..+?\\K0+$/', "", $result);
return bcdiv($result, $subArg, $precision);
}
}
You can try it on
http://sandbox.onlinephpfunctions.com/code/333f58c1487e86e53dde64c26930b57e1f9e0fe8
You are over complicating a simple task:
function cutAtPrecision($number, $precision) {
return intval($number * (1 / $precision)) / (1 / $precision);
}
echo cutAtPrecision(323.659, 0.01); // 323.65

Rounding function

I'm trying to round a number to 1 decimal place.
I've written this code to shorten numbers over 1000:
$numbers = array($count);
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k";
}
}
foreach($numbers as $number) {
echo "Posts: ";
echo format_number($number);
}
For example this makes 15900 15.900k. Now I made a rounding part so it only reads as 15.9k:
$rounded = round($number, 1); // e.g. 66.7346 becomes 66.7
and echoed it:
echo "".$rounded."";
but nothing shows.
Any ideas?
You aren't returning anything if the number is less than 1000.
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k";
}
//What happens here if the number is not 1000?
}
I'd rewrite it as this:
function format_number($number) {
$append = '';
if($number >= 1000) {
$number /= 1000;
$append = 'k';
}
return round($number, 1) . $append;
}

PHP generate a random minus or plus percentage of a given value

I have a value, lets say its 1000.
Now I have to generate a random minus or plus percentage of 1000.
In particular I have to generate or a -20% of 1000 or a +20% of 1000 randomly.
I tried using rand() and abs() but with no success..
Is there a way in PHP to achieve the above?
A bit of basic mathematics
$number = 1000;
$below = -20;
$above = 20;
$random = mt_rand(
(integer) $number - ($number * (abs($below) / 100)),
(integer) $number + ($number * ($above / 100))
);
rand(0, 1) seems to work fine for me. Maybe you should make sure your percentage is in decimal format.
<?php
$val = 10000;
$pc = 0.2;
$result = $val * $pc;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
?>
$number = 10000;
$percent = $number*0.20;
$result = (rand(0,$percent)*(rand(0,1)*2-1));
echo $result;
Or if you want some sort of running balance type thing....
function plusminus($bank){
$percent = $bank*0.20;
$random = (rand(0,$percent)*(rand(0,1)*2-1));
return $bank + $random;
}
$new = plusminus(10000);
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
I know this is really old now but stumbled across it looking for something similar where I needed a random sign (+ or -) so opted for a random boolean:
<?php $sign = (rand(0,1) == 1) ? '+' : '-'; ?>
Thanks to this this answer.
So I would opt for a solution like this:
<?php
// Alter these as needed
$number = 1000;
$percentage = 20;
// Calculate the change
$change_by = $number * ($percentage / 100);
// Set a boolean at random
$random_boolean = rand(0,1) == 1;
// Calculate the result where we are using plus if true or minus if false
$result = ($random_boolean) ? $number + $change_by : $number - $change_by;
// Will output either 1200 or 800 using these numbers as an example
echo $result;
?>

Converting float decimal to fraction

I am trying to convert calculations keyed in by users with decimal results into fractions. For e.g.; 66.6666666667 into 66 2/3. Any pointers?
Thanx in advance
Continued fractions can be used to find rational approximations to real numbers that are "best" in a strict sense. Here's a PHP function that finds a rational approximation to a given (positive) floating point number with a relative error less than $tolerance:
<?php
function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);
return "$h1/$k1";
}
printf("%s\n", float2rat(66.66667)); # 200/3
printf("%s\n", float2rat(sqrt(2))); # 1393/985
printf("%s\n", float2rat(0.43212)); # 748/1731
I have written more about this algorithm and why it works, and even a JavaScript demo here: https://web.archive.org/web/20180731235708/http://jonisalonen.com/2012/converting-decimal-numbers-to-ratios/
Farey fractions can be quite useful in this case.
They can be used to convert any decimal into a fraction with the lowest possible denominator.
Sorry - I don't have a prototype in PHP, so here's one in Python:
def farey(v, lim):
"""No error checking on args. lim = maximum denominator.
Results are (numerator, denominator); (1, 0) is 'infinity'."""
if v < 0:
n, d = farey(-v, lim)
return (-n, d)
z = lim - lim # Get a "zero of the right type" for the denominator
lower, upper = (z, z+1), (z+1, z)
while True:
mediant = (lower[0] + upper[0]), (lower[1] + upper[1])
if v * mediant[1] > mediant[0]:
if lim < mediant[1]:
return upper
lower = mediant
elif v * mediant[1] == mediant[0]:
if lim >= mediant[1]:
return mediant
if lower[1] < upper[1]:
return lower
return upper
else:
if lim < mediant[1]:
return lower
upper = mediant
Converted Python code in answer from #APerson241 to PHP
<?php
function farey($v, $lim) {
// No error checking on args. lim = maximum denominator.
// Results are array(numerator, denominator); array(1, 0) is 'infinity'.
if($v < 0) {
list($n, $d) = farey(-$v, $lim);
return array(-$n, $d);
}
$z = $lim - $lim; // Get a "zero of the right type" for the denominator
list($lower, $upper) = array(array($z, $z+1), array($z+1, $z));
while(true) {
$mediant = array(($lower[0] + $upper[0]), ($lower[1] + $upper[1]));
if($v * $mediant[1] > $mediant[0]) {
if($lim < $mediant[1])
return $upper;
$lower = $mediant;
}
else if($v * $mediant[1] == $mediant[0]) {
if($lim >= $mediant[1])
return $mediant;
if($lower[1] < $upper[1])
return $lower;
return $upper;
}
else {
if($lim < $mediant[1])
return $lower;
$upper = $mediant;
}
}
}
// Example use:
$f = farey(66.66667, 10);
echo $f[0], '/', $f[1], "\n"; # 200/3
$f = farey(sqrt(2), 1000);
echo $f[0], '/', $f[1], "\n"; # 1393/985
$f = farey(0.43212, 2000);
echo $f[0], '/', $f[1], "\n"; # 748/1731
Based upon #Joni's answer, here is what I used to pull out the whole number.
function convert_decimal_to_fraction($decimal){
$big_fraction = float2rat($decimal);
$num_array = explode('/', $big_fraction);
$numerator = $num_array[0];
$denominator = $num_array[1];
$whole_number = floor( $numerator / $denominator );
$numerator = $numerator % $denominator;
if($numerator == 0){
return $whole_number;
}else if ($whole_number == 0){
return $numerator . '/' . $denominator;
}else{
return $whole_number . ' ' . $numerator . '/' . $denominator;
}
}
function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);
return "$h1/$k1";
}
Based on #APerson's and #Jeff Monteiro's answers I've created PHP version of Farey fractions that will be simplified to whole values with fractions with lowest possible denominator:
<?php
class QuantityTransform
{
/**
* #see https://stackoverflow.com/questions/14330713/converting-float-decimal-to-fraction
*/
public static function decimalToFraction(float $decimal, $glue = ' ', int $limes = 10): string
{
if (null === $decimal || $decimal < 0.001) {
return '';
}
$wholeNumber = (int) floor($decimal);
$remainingDecimal = $decimal - $wholeNumber;
[$numerator, $denominator] = self::fareyFraction($remainingDecimal, $limes);
// Values rounded to 1 should be added to base value and returned without fraction part
if (is_int($simplifiedFraction = $numerator / $denominator)) {
$wholeNumber += $simplifiedFraction;
$numerator = 0;
}
return (0 === $wholeNumber && 0 === $numerator)
// Too small values will be returned in original format
? (string) $decimal
// Otherwise let's format value - only non-0 whole value / fractions will be returned
: trim(sprintf(
'%s%s%s',
(string) $wholeNumber ?: '',
$wholeNumber > 0 ? $glue : '',
0 === $numerator ? '' : ($numerator . '/' . $denominator)
));
}
/**
* #see https://stackoverflow.com/a/14330799/842480
*
* #return int[] Numerator and Denominator values
*/
private static function fareyFraction(float $value, int $limes): array
{
if ($value < 0) {
[$numerator, $denominator] = self::fareyFraction(-$value, $limes);
return [-$numerator, $denominator];
}
$zero = $limes - $limes;
$lower = [$zero, $zero + 1];
$upper = [$zero + 1, $zero];
while (true) {
$mediant = [$lower[0] + $upper[0], $lower[1] + $upper[1]];
if ($value * $mediant[1] > $mediant[0]) {
if ($limes < $mediant[1]) {
return $upper;
}
$lower = $mediant;
} elseif ($value * $mediant[1] === $mediant[0]) {
if ($limes >= $mediant[1]) {
return $mediant;
}
if ($lower[1] < $upper[1]) {
return $lower;
}
return $upper;
} else {
if ($limes < $mediant[1]) {
return $lower;
}
$upper = $mediant;
}
}
}
}
Then you san use it like:
QuantityTransform::decimalToFraction(0.06); // 0.06
QuantityTransform::decimalToFraction(0.75); // 3/4
QuantityTransform::decimalToFraction(1.75, ' and '); // 1 and 3/4
QuantityTransform::decimalToFraction(2.33, ' and '); // 2 and 1/3
QuantityTransform::decimalToFraction(2.58, ' ', 5); // 2 3/5
QuantityTransform::decimalToFraction(2.58, ' & ', 10); // 2 & 4/7
QuantityTransform::decimalToFraction(1.97); // 2
Here is my approach to this problem. Works fine with rational numbers.
function dec2fracso($dec){
//Negative number flag.
$num=$dec;
if($num<0){
$neg=true;
}else{
$neg=false;
}
//Extracts 2 strings from input number
$decarr=explode('.',(string)$dec);
//Checks for divided by zero input.
if($decarr[1]==0){
$decarr[1]=1;
$fraccion[0]=$decarr[0];
$fraccion[1]=$decarr[1];
return $fraccion;
}
//Calculates the divisor before simplification.
$long=strlen($decarr[1]);
$div="1";
for($x=0;$x<$long;$x++){
$div.="0";
}
//Gets the greatest common divisor.
$x=(int)$decarr[1];
$y=(int)$div;
$gcd=gmp_strval(gmp_gcd($x,$y));
//Calculates the result and fills the array with the correct sign.
if($neg){
$fraccion[0]=((abs($decarr[0])*($y/$gcd))+($x/$gcd))*(-1);
}else{
$fraccion[0]=(abs($decarr[0])*($y/$gcd))+($x/$gcd);
}
$fraccion[1]=($y/$gcd);
return $fraccion;
}
Sometimes it is necessary to treat only the decimals of a float. So I created a code that uses the function created by #Joni to present a format that is quite common in culinary recipes, at least in Brazil.
So instead of using 3/2 which is the result for 1.5, using the function I created it is possible to present the value 1 1/2, and if you want, you can also add a string to concatenate the values, creating something like "1 and 1/2 ".
function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);
return "$h1/$k1";
}
function float2fraction($float, $concat = ' '){
// ensures that the number is float,
// even when the parameter is a string
$float = (float)$float;
if($float == 0 ){
return $float;
}
// when float between -1 and 1
if( $float > -1 && $float < 0 || $float < 1 && $float > 0 ){
$fraction = float2rat($float);
return $fraction;
}
else{
// get the minor integer
if( $float < 0 ){
$integer = ceil($float);
}
else{
$integer = floor($float);
}
// get the decimal
$decimal = $float - $integer;
if( $decimal != 0 ){
$fraction = float2rat(abs($decimal));
$fraction = $integer . $concat . $fraction;
return $fraction;
}
else{
return $float;
}
}
}
Usage e.g:
echo float2fraction(1.5);
will return "1 1/2"

Categories