Negative and positive percentage range [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to get a percentage of the current value between -60 and 60.
so if
NUMBER == 60
The result should be 100%
NUMBER == -60
The result should be 0%
NUMBER == 0
The result should be 50%
How i can achieve this?

This is basic math
MIN = -60;
MAX = 60;
if (NUMBER < MIN) {
PERCENT = 0;
}
else if (NUMBER > MAX) {
PERCENT = 100;
}
else {
PERCENT = (NUMBER-MIN)*100/(MAX-MIN);
}

(NUMBER + 60) / 1.2
Or
(NUMBER + 60) / 120 / 100

Related

generate unqie life time random number for each number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I had a list of numbers between 1 until 100000000
I need a function to get a number and return a random number between 1 to 30 For each other number...But unique!
for example each time I call getrandomnumber('3') it always return me 25! (no other random number between 1 ...30 like rand(1,30)
You could use the given number as a seed and generate the number using the standard random number function.
function getrand($num)
{
srand($num);
return rand(1 , 30);
}
The Answer
for($i = 0;$i<100;$i++)
echo '<br>'.getrand($i);
function getrand($num)
{
$num = substr($num, -2);
$num = (int)$num;
if($num < 31) return $num;
elseif($num > 30 && $num < 61) return $num - 30;
elseif($num > 60 && $num < 91) return $num - 60;
elseif($num > 90 && $num < 101) return $num - 70;
}
1-30 repeat:
$x=0;
for($i = 0;$i<100;$i++){
$x++;
if($x>30)$x=1;
echo '<br>'.$x;
}

first 500 from 1300 and do something with it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say we have 1300 units. now i want to first 500 units to be a price of 12 euro and the other 800 a price of 9 euro.
So what i basicly need is a function that calculates over a total number and gets the first 500 * 12 euro and the other 800 * 9 euro. But the total unit can also be 5000 instead of this example of 1300 unit, its user input ofcouse.
<?php
$total = 1300;
$sub = 1;
$sub2 = 1;
for($i=1; $i<=$total; $i++)
{
for($sub; $i<=500; $sub++)
{
$sub2 += $sub;
break;
}
}
echo $sub2;
?>
<?php
$units = 1300;
$cut_off=500;//made it a var just in case
$a=12;//fist 500
$b=9;//rest
//verbose calculations, you don't really need to create the extra variables
$a_total=$cut_off*$a;
$b_total=($units-$cut_off)*$b;
$grand_total=$a_total+$b_total;
?>
you should probably add a check if the units imputed is less than 500. or things will turn out weird
Updated:
<?php
function calc_total($qtt, $min = 500, $fst_price = 12, $nxt_price = 9){
if($qtt > $min) return false; // Check if the minimum 500 was sent.
/* First calculate the rest, which is the quantity sent by
the user without the minimum x 9. Then add the minimum x 12 */
$total = (($qtt - $min) * 9) + ($min * 12);
return $total;
}
?>
You can call the function like this:
echo "$". calc_total(1300);

How to display total number from start to a range in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to display total number from start to end in a range in php .Each time number will increase by 10 .
For example total number is 35
Then number will display this way :
1 to 10
11 to 20
21 to 30
31 to 35
$num=35;
for($i=1;$i<$num;$i=$i+10)
{
$j=$i+9;
if($j>$num)
$j=$num;
echo $i.' to '. $j;
}
try
$num=35;
for($i=1;$i<=$num;$i++) {
if($i %10 == 0)
echo $i.'<br>';
else
echo $i;
}
will output :-
12345678910
11121314151617181920
21222324252627282930
3132333435
Use a for loop like this
for($x = 1; $x <= 35, $x += 10){
echo $x . " to " . $x+9;
}
This will work i hope.
Thanks

Divide integer number into 3 parts using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to divide an integer value into 3 parts using php
for eg: I have a value 9 the answer is like 3 + 3 + 3
if i have value 10 the ans like 3 + 3 + 4 or something like that
You can use a modulus function.
<?php
$yourInt=10;
$remainder=$yourInt % 3;
$third=floor($yourInt/3);
$lastBit=$third+$remainder;
echo "The numbers are $third + $third + $lastBit.";
?>
Output:
The numbers are 3 + 3 + 4.
If you have a value that is >= 3 you can do the following:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11;
$d = [$num/3,$num/3,$num/3];
$round = array_map('round', $d);
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0);

What math to use in this example? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What math can you use in this following example:
I want to divide a number by 2, but the result should always be a whole number.That means I cannot divide by 2 when the number is odd.
For ex.
when I divide 5 by 2.I want the output as 3 or 2, but not 2.5
Use the math functions ceil or floor:
$number = 5 / 2;
echo ceil( $number); // Outputs 3
echo floor( $number); // Outputs 2
$number = 4 / 2;
echo ceil( $number); // Outputs 2
echo floor( $number); // Outputs 2
Demo
$n = 5;
floor($n / 2); // 2
ceil($n / 2); // 3
$n = 6;
floor($n / 2); // 3
ceil($n / 2); // 3

Categories