How to format number 300 to 3.0 in php - php

I have a MySQL table which contains whole number.
Units
300,
900,
600,
125,
225,
I want it to display using PHP like thus
Units
3.0,
9.0,
6.0,
1.25,
2.25,
I have tried using number_format but doesn't do the trick.

This is basic math:
echo $num * .01;
If you always want two decimal points:
echo number_format($num * .01, 2, '.', '');
If you want one decimal for x00 numbers just use make the third parameter a variable that is either 1 or 2 based on the last two digits of the original number or use the modulus operator to determine if the number is divisible by 100.
$round = ($num % 100) ? 2 : 1;
echo number_format($num * .01, $round, '.', '');

Related

How to transform last two numbers to decimal (int to float) in php?

I need transform 100 to 1.00, 345 to 3.45 or any number with 3 digits or more to record in db like a decimal.
Don't need add .00, just transform last two numbers in decimal.
I try number_format($num, 2) but is wrong.
It seems, that you want to divide your numbers to 100, so using number_format() with the appropriate $decimal_separator and $thousands_separator is an option:
<?php
echo number_format($num / 100, 2, '.', '');
?>
Since you are converting from an integer to a float, you can achieve this simply by dividing the number by 100.
$input = 100;
$value = floatval($input) / 100;
$value = number_format($value, 2);
echo $value;
You may then use number_format (as you were using before) to force two decimal places after any evenly divided float numbers (such as 100).
Demo: PHP Sandbox Example

PHP how to add trailing zeros if int is less then 4 digits

Good day. I am converting a decimal (10, 2) from my mySql database to an int like below
$totalprice = number_format($price, 0, ".", "");
The issue I am running into is that if the int has no numbers after the decimal place it will convert to a smaller number. For example, if a price of 35.00 is converted to an int using the logic above, it will be 35 instead of 3500 (35 cents instead of 35 dollars). Is there a way in php to add trailing zeros if needed?
Might want to take a look at BC Math extension. https://www.php.net/manual/en/ref.bc.php
<?php
echo bcmul(35.00, 100);
?>
Output : 3500
You can use STR_PAD_RIGHT
example :
$number = 35;
$number = str_pad($number, 4, '0', STR_PAD_RIGHT);

Set precision for a float number in PHP

I get a number from database and this number might be either float or int.
I need to set the decimal precision of the number to 3, which makes the number not longer than (regarding decimals) 5.020 or 1518845.756.
Using PHP
round($number, $precision)
I see a problem:
It rounds the number. I need a function to only cut the decimals short, without changing their values which round( ) seems not to follow.
You can use number_format() to achieve this:
echo number_format((float) $number, $precision, '.', '');
This would convert 1518845.756789 to 1518845.757.
But if you just want to cut off the number of decimal places short to 3, and not round, then you can do the following:
$number = intval($number * ($p = pow(10, $precision))) / $p;
It may look intimidating at first, but the concept is really simple. You have a number, you multiply it by 103 (it becomes 1518845756.789), cast it to an integer so everything after the 3 decimal places is removed (becomes 1518845756), and then divide the result by 103 (becomes 1518845.756).
Demo
Its sound like floor with decimals. So you can try something like
floor($number*1000)/1000
If I understand correctly, you would not want rounding to occur and you would want the precision to be 3.
So the idea is to use number_format() for a precision of 4 and then remove the last digit:
$number = '1518845.756789';
$precision = 3;
echo substr(number_format($number, $precision+1, '.', ''), 0, -1);
Will display:
1518845.756
rather than:
1518845.757
Links : number_format() , substr()
See this answer for more details.
function numberPrecision($number, $decimals = 0)
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = pow(10, $decimals);
return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}
$num=5.1239;
$testnum=intval($num*1000)/1000;
echo $testnum; //return 5.123

PHP array product combinations

I have produced an array of prime factors of a number - this is supposed to be the hard part! However, in order to create a list of the divisors of the same number the prime factors need to be combined in every which possible way. Something that I'm struggling to do with php.
For example I have an array of:
2
2
2
3
3
41
53
...for the number 156456; multiply them all together and you get back to the number. What I need to do is to multiply all of the duos together e.g. 2x2, 2x3, 2x53 etc and then all of the triplets together and so on until I finally multiply the 7 blocks of six together.
As you can see this will give a very large array with all of the divisors in, 4, 6, 8, 9, 12 etc. with many duplicates. I just can't seem to get from the array I have above to this array of divisors that I want. It's a case of multiplying out every possible combination of elements in the array, is there a php function for this, my search so far has been fruitless?
After reading this page: http://mathcentral.uregina.ca/QQ/database/QQ.02.06/joe1.html, I tried to build something that might work, It may not be the most efficient solution and its also limited to count($primes) <= 32 on 32 bit systems. If you need more, feel free to use a Bitset:
$primes = Array(2, 2, 2, 3, 3, 41, 53);
$num_primes = count($primes); // 7, if this is over 32, it won't work on 32bit systems
$divisors = Array();
// number of possible combinations
$limit = pow(2, $num_primes) - 1; // 127
// count a number up and use the binary
// representation to say which index is
// part of the current divisor
for($number = 0; $number <= $limit; $number++) {
$divisor = 1;
// only multiply activated bits in $number to the divisor
for($i = 0; $i < $num_primes; $i++) {
$divisor *= ($number >> $i) & 1 ? $primes[$i] : 1;
}
$divisors[] = $divisor;
}
echo implode(", ", array_unique($divisors));
This results into the following divisors:
1, 2, 4, 8, 3, 6, 12, 24, 9, 18, 36, 72, 41, 82, 164, 328, 123, 246, 492,
984, 369, 738, 1476, 2952, 53, 106, 212, 424, 159, 318, 636, 1272, 477,
954, 1908, 3816, 2173, 4346, 8692, 17384, 6519, 13038, 26076, 52152, 19557,
39114, 78228, 156456
To find all divisors you need to multiply each prime factor with each other in every possible combination. To do this I calculate the number of possible combinations ($limit). If you now count a number up to this limit the binary representation looks something like this:
7 bit
<----->
0000000 0
0000001 1
0000010 2
0000011 3
0000100 4
0000101 5
0000110 6
0000111 7
0001000 8
0001001 9
...
1111110 126
1111111 127
The current binary representation of $number represents which indexes of $primes are used to calculate the current $divisor. To show this better let's say $number = 5, which is 0000101 in binary. And the calculation for $divisor would be 2 * 1 * 2 * 1 * 1 * 1 * 1 = 4. Only the first and the third bit is set, so only the first and the third element in the array is used for the calculation.
I hope this makes it a little bit clearer.

Generating a random number but with a twist

I need to generate a random number.
But the twist is that the % of lower numbers should be greater than the higher.
For example.
rand > 1 to 100
13,
15,
12,
16,
87,
15,
27,
12,
1,
12,
98,
12,
53,
12,
14....
The bold integers will be the ones return from the 1-100 range.
The math should be like so rand = a number lower than max/2
Hope you guys can help.
Ps, How would a matrix come into this ? im not superior at maths :(
The abs answer seems to be the one.
$up = $down = 0;
while(true)
{
if(abs((rand()%150)-50) < 50)
{
$up++;
}else
{
$down++;
}
if( ($up + $down) == 500){ break;}
}
echo $up . '/' . $down;
how about
n = abs((rand()%150)-50)
$x = rand(0,1) ? rand(1,100) : rand(1,50);
Simple method: the first rand(0,1) selects between the two cases. Either 1..50 or 1..100 as random range. Since 1,100 already encompases 1,50, the latter range is selected 100% of the time, the former case only in 1 of 2 runs.
If you want a distribution where the highest numer 99 gets selected almost never, but the lower numbers 1..20 pretty frequent, then a simple rand(1,rand(1,100)) would do.
$rand = (rand() * rand()) / (getrandmax() * getrandmax());
This will give you a random number between 0 and 1 with a high probability of falling into the lower end of the range and the probability of a larger number decreasing exponentially. You can then scale this result to any range that you want.

Categories