How to make a digit round? - php

I want 3000 for all these numbers:
3001 - 3500 - 3999
I want 40000 for all these numbers:
40000.3 - 40101 - 48000.8 - 49901
I want 20 for all these numbers:
21 - 25.2 - 29
There is two PHP function to make a digit round (floor and round) But none of them don't act exactly what I need.
Note: I don't know my number is contains how many digits. If fact it is changing.
Is there any approach to do that?

There are "many" ways how to achieve this. One is this:
<?php
echo roundNumber( 29 )."<br>";
echo roundNumber( 590 )."<br>";
echo roundNumber( 3670 )."<br>";
echo roundNumber( 49589 )."<br>";
function roundNumber( $number )
{
$digitCount = floor( log10( $number ) ) ;
$base10 = pow( 10, $digitCount );
return floor( $number / $base10 ) * $base10;
}
?>
The output is this:
20
500
3000
40000

This will work for any no. of digits. Try this:
$input = 25.45;
if (strpos($input, ".") !== FALSE) {
$num = explode('.', $input);
$len = strlen($num[0]);
$input = $num[0];
} else {
$len = strlen($input);
}
$nearest_zeroes = str_repeat("0", $len-1);
$nearest_round = '1'.$nearest_zeroes;
echo floor($input/$nearest_round) * $nearest_round;
The idea is when you round a 4 digit no, say 3999, $nearest_round should be 1000.
For 39990(5 digit), $nearest_round = 10000.
For 25(2 digit), $nearest_round = 10.
And so on.
So, the idea is to generate $nearest_round dynamically based on the no. of digits of $input.
Hope this helps.

Related

PHP random INT with evenly distributed order of magnitude results

PHP rand(min, max) between 1 and 9999 gives almost all results with 4 digits (because there are ~90% of the numbers with 4 digits). So, if I ran it 1000 times, roughly ~90% of them will probably have 4 digits.
Is there a way to generate a random INT from 1 to 9999 and that the output number have the same chance of having 1, 2, 3 or 4 digits without doing it manually?
By doing it manually I mean like this:
$digits = rand(1, 4);
$num = '';
for($i = 0; $i < $digits; $i++){
$num .= rand(0, 9);
}
$final = intval($num);
So, if I ran it 1000 times, roughly ~90% of them will probably have 4 digits.
That's exactly how uniform distributions work. There's no out of the box function to do what you're after, so you have to make some statistics magic.
What I'm thinking of is: generate a random number between 0 and 1. If it's between 0 and .25, generate another random number between 0 and 9. If it's between .25 and .5, generate another random number between 10 and 99, and so on and so forth. Then, you'd have 1/4 chance of getting each order of magnitude.
This will obviously have a bias towards the lower numbers though, since there are less of them. For example, 1 has a 25% / 10 = 2.5% chance, while 1001 has a 25% / 8998 = 0.00277% chance.
It'd go something like this:
<?php
$initial = rand(0, 100)/100;
if ($initial < .25) {
$random = rand(0, 9);
}
elseif ($initial < .5) {
$random = rand(10, 99);
}
elseif ($initial < .75) {
$random = rand(100, 999);
}
elseif ($initial >= .75) {
$random = rand(1000, 9999);
}
var_dump($random);
Demo

Format number to N significant digits in PHP

I would like to format (round) float (double) numbers to lets say 2 significant digits for example like this:
1 => 1
11 => 11
111 => 110
119 => 120
0.11 => 0.11
0.00011 => 0.00011
0.000111 => 0.00011
So the arbitrary precision remains same
I expect there is some nice function for it already built in, but could not find any so far
I was pointed to How to round down to the nearest significant figure in php, which is close but doesn't work for N significant digits and I'm not sure what it does with 0.000XXX numbers
To get a number rounded to n significant figures you need to find the size of the number in powers of ten, and subtract that from n.
This works fine for simple rounding:
function sigFig($value, $digits)
{
if ($value == 0) {
$decimalPlaces = $digits - 1;
} elseif ($value < 0) {
$decimalPlaces = $digits - floor(log10($value * -1)) - 1;
} else {
$decimalPlaces = $digits - floor(log10($value)) - 1;
}
$answer = round($value, $decimalPlaces);
return $answer;
}
This will give the following:
0.0001234567 returns 0.0001235
123456.7 returns 123500
However a value such as 10 to four significant figures should strictly be represented as 10.00 to signify the precision to which the value is known.
If this is the desired output you can use the following:
function sigFig($value, $digits)
{
if ($value == 0) {
$decimalPlaces = $digits - 1;
} elseif ($value < 0) {
$decimalPlaces = $digits - floor(log10($value * -1)) - 1;
} else {
$decimalPlaces = $digits - floor(log10($value)) - 1;
}
$answer = ($decimalPlaces > 0) ?
number_format($value, $decimalPlaces) : round($value, $decimalPlaces);
return $answer;
}
Now 1 is displayed as 1.000
With little modification to possible duplicate, answer by Todd Chaffee:
public static function roundRate($rate, $digits)
{
$mod = pow(10, intval(round(log10($rate))));
$mod = $mod / pow(10, $digits);
$answer = ((int)($rate / $mod)) * $mod;
return $answer;
}
To make sigFig(0.9995, 3) output 1.00, use
if(floor(log10($value)) !== floor(log10(round($value, $decimalPlaces)))) {$decimalPlaces--;}
Said line of code should be placed before declaring $answer.
If input $value is negative, set a flag and remove the sign at the beginning of the function, like this:
if($value < 0){$flag = 1;}
$value = ltrim($value, "-");
Then right before returning $answer, detect if the flag is set and if so restore the negative sign, like this:
if(isset($flag)){$answer = "-".$answer;}
Finally, for result values with ambiguous number of significant digits (e.g., 1000, 12000,...), express the result in scientific notation to the desired number of significant digits using sprintf or printf.

Php equation: variable minus same rounded variable, possible?

I need to echo number(variable) in two ways and i need help with code for this equation.
Example:
Variable is 5003
First echo has to be: 5000 (rounded)
Second echo has to be just the rounded digits: 3
So i want to know if and how can i achieve this equation, im thinking among lines of: variable(5003) minus rounded variable(5000) equals 3
So that way if variable is lets say 15009
Fist will be 15000
Second will be 9
I hope this make sense, thank you for help
You should look into the roundPHP function:
You can have negative decimal points like this:
round(5003, -3); // returns 5000
round(15009, -3); // returns 15000
To figure out the difference you can do like this:
$input = 5003
$x = $input;
$y = round($input, -3);
$z = $x - $y; // z is now 3
PHP is not a mathematical language, so it cannot solve equations for you.
You can make a more general solution like this:
$inputs = [
5003,
15009,
55108,
102010
];
foreach ($inputs as $input) {
$decimals = floor(log10($input)) - 1;
$rounded = round($input, -1 * $decimals);
echo "$input - $rounded = " . ($input - $rounded) . PHP_EOL;
}
Outputs:
5003 - 5000 = 3
15009 - 15000 = 9
55108 - 55000 = 108
102010 - 100000 = 2010
Assuming that you want to round the last three digits:
$input = 5003;
$rounded = (int)(5003 / 1000) * 1000;
$rest = $input - $rounded;
echo($rounded . "\n" . $rest);
This results in:
5000
3

PHP - Get length of digits in a number

I would like to ask how I can get the length of digits in an Integer. For example:
$num = 245354;
$numlength = mb_strlen($num);
$numlength should be 6 in this example. Somehow I can't manage it to work?
Thanks
EDIT: The example code above --^ and its respective method mb_strlen(); works just fine.
Maybe:
$num = 245354;
$numlength = strlen((string)$num);
Accepted answer won't work with the big numbers. The better way to calculate the length of any number is to invoke floor(log10($num) + 1) with a check for 0.
$num = 12357;
echo $num !== 0 ? floor(log10($num) + 1) : 1; // prints 5
It has multiple advantages. It's faster, you don't do the casting of types, it works on big numbers, it works with different number systems like bin, hex, oct.
The equation does the logarithm with base 10 then makes the floor of it and adds 1.
This solution can work independently on the base, so if you want to calculate the length of binary or hex just change the base of the logarithm.
Working fiddle
The accepted solution presents a problem when evaluating negative numbers.
It works with a positive number:
$num = 245354;
$numlength = strlen((string)$num);
// Result: 6
But with a negative number, the (-) is added to the count:
$num = -245354;
$numlength = strlen((string)$num);
// Result: 7
Quick workaround:
$num = -245354;
$numlength = strlen((string)abs($num));
// Result: 6
More elegant way :)
ceil(log10($num));
You could also use some basic math!
$digits = (int)(log($num,10)+1)
<?php
$num = 123;
$num2 = 1234;
$num3 = 12345;
function digits($num){
return (int) (log($num, 10) + 1);
}
echo "\n $num: " . digits($num); // 123: 3
echo "\n $num2:" . digits($num2); // 1234: 4
echo "\n $num3:" . digits($num3); // 12345: 5
echo "\n";
Another way to find out the length of a number in digits would be to divide the integer part of the number to 10 until it becomes 0.
Example:
2021/10 = 202.1
202/10 = 20.2
20/10 = 2
2/10 = 0.2
Code:
function numberGetLength($number) {
$count = 0;
while (intval($number) > 0) {
$number = intval($number) / 10;
$count += 1;
}
return $count
}
Just using some version of (int)(log($num,10)+1) fails for 10, 100, 1000, etc. It counts the number 10 as 1 digit, 100 as two digits, etc. It also fails with 0 or any negative number.
If you must use math (and the number is non-negative), use:
$numlength = (int)(log($num+1, 10)+1);
Or for a math solution that counts the digits in positive OR negative numbers:
$numlength = ($num>=0) ? (int)(log($num+1, 10)+1) : (int)(log(1-$num, 10)+1);
But the strlen solution is just about as fast in PHP.
In PHP types are loosely set and guessed, if you want to see something as a string if it is an integer, float, and (i have not tried this) bool then #Gorjunav is the most correct answer.
Reset the variable as a string
$stringNum = (string) $num;
Then you can go anything string related you want with it! And vice-versa for changing a string to an int
$number = (int) $stringNum;
and so on...
count only integer value
`<?php
$n1 =12345;
$n2 =123454.55;
$n3 =12345564.557;
echo "The Number you Type: ".$n1."<br>";
$count = 0;
while ($n1 != 0)
{
$n1 = $n1 / 10;
$n1 = intval($n1);
++$count;
}
echo "The Digit in a Number: ".$count;
}
?>`
echo strlen((string) abs($num)); // using **abs** it'll work with negative integers as well
Tested in PHP 4.4.9 - 8.0.0
$array = array(-1, 0, -0, 1, 4, 9, 10, -10, 20, -20, 100, -100);
foreach( $array as $key => $num ){
echo $key."\t{$num}\t=>\t".($num !== 0 ? floor(log10(abs($num)) + 1) : 1)."\n";
}
/* Output:
0 -1 => 1
1 0 => 1
2 0 => 1
3 1 => 1
4 4 => 1
5 9 => 1
6 10 => 2
7 -10 => 2
8 20 => 2
9 -20 => 2
10 100 => 3
11 -100 => 3
*/
The following function work for either integers or floats (works with PHP7+):
function digitsCount($number): int
{
$number = abs($number);
$numberParts = explode(".", $number);
return
strlen($numberParts[0]) +
(strlen($numberParts[1] ?? 0));
}

No Truncate function in PHP - Options?

I have an algorithm that performs the following calculations:
( ( 0.50 * 0 ) + 7 ) / 10 = 0.70
( ( 0.70 * 10 ) + 9 ) / 20 = 0.80
( ( 0.80 * 20 ) + 7 ) / 30 = 0.7666666667 -> I want this value to truncate to 0.76
So that it may feed into the rest of the calculation as:
4a. ( ( 0.76 * 30 ) + 8 ) / 40 = 0.77
And not to feed in when rounded up two decimal places as 0.77:
4b. ( ( 0.77 * 30 ) + 8 ) / 40 = 0.77
****The following seem to have failed and instead force a round up to 0.77:****
The PHP sptrinf() function: PHP dropping decimals without rounding up
The PHP number_format() function: PHP: show a number to 2 decimal places
The PHP floor() function: Truncate float numbers with PHP
Is there another way?
Is it at all possible to achieve what I want (truncate to two decimal places) with PHP?
Please assist. Many thanks.
UPDATE - SOLVED
Ok, it is working now thanks to dkamins and zneak. I used the floor() approach (I assume I wasn't doing something right in the past). However, now the following happens:
e.g.
(0.86 * 30) + 9 ) / 40 = 0.87 (it should), yet after TRUNC it = 0.86
How is it truncating 0.87 to 0.86? It makes no sense. Is there a way to get it to truncate only if there are more than 2 decimal places?
SOLVED:
$numDecPlace = strlen(substr(strrchr($newRel, "."), 1));
echo '<p>Test: Number of decimal places=' .$numDecPlace. '</p>';
if($numDecPlace > 2) {
$newRel = floor($newRel * 100) / 100; // Truncate to 2dp.
echo '<p>Test: New relationship truncated is $newRel=' .$newRel. '</p>';
}
All the answers given here round the number.
This function worked for me and i hope for you too.
function truncate($number, $decimals)
{
$point_index = strrpos($number, '.');
return substr($number, 0, $point_index + $decimals+ 1);
}
Where $number is the number to truncate and$decimals is the number of decimals you want.
Example: truncate(-38.59540719940386 , 6); returns -38.595407
If you want, check substr's documentation!
You can use PHP's floor function along with some decimal shifting like so:
floor(0.7666666667 * 100) / 100;
how about 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
?>
To do this accurately for both +ve and -ve numbers you need use:
- the php floor() function for +ve numbers
- the php ceil() function for -ve numbers
function truncate_float($number, $decimals) {
$power = pow(10, $decimals);
if($number > 0){
return floor($number * $power) / $power;
} else {
return ceil($number * $power) / $power;
}
}
the reason for this is that floor() always rounds the number down, not towards zero.
ie floor() effectively rounds -ve numbers towards a larger absolute value
eg floor(1.5) = 1 while floor(-1.5) = 2
Therefore for the truncate method using multiply by power, round, then divide by power:
- floor() only works for positive numbers
- ceil() only works for negative numbers
To test this, copy the following code into the editor of http://phpfiddle.org/lite (or similar):
<div>Php Truncate Function</div>
<br>
<?php
function truncate_float($number, $places) {
$power = pow(10, $places);
if($number > 0){
return floor($number * $power) / $power;
} else {
return ceil($number * $power) / $power;
}
}
// demo
$lat = 52.4884;
$lng = -1.88651;
$lat_tr = truncate_float($lat, 3);
$lng_tr = truncate_float($lng, 3);
echo 'lat = ' . $lat . '<br>';
echo 'lat truncated = ' . $lat_tr . '<br>';
echo 'lat = ' . $lng . '<br>';
echo 'lat truncated = ' . $lng_tr . '<br><br>';
// demo of floor() on negatives
echo 'floor (1.5) = ' . floor(1.5) . '<br>';
echo 'floor (-1.5) = ' . floor(-1.5) . '<br>';
?>
I used would use the round() function as mentioned by Kartikey. You can find more information here at PHP.Net
float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
Fissh

Categories