Check Positive Float value in php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a variable as $x=some_float_value. So how can I validate or check that $x is a Positive Float.
For Ex.
$x=-4.99;
function checkFloat($x){
/////return true if it is Positive
}
Please Tell me how can i do this.

Use filter_var() with FILTER_VALIDATE_FLOAT:
if (filter_var($x, FILTER_VALIDATE_FLOAT) && $x > 0) {
// ok
}
See it in action

Check if it is a float with is_float and > 0:
function checkFloat($x) {
return (is_float($x) && $x > 0);
}

I do not see the issue...
return $x > 0;
To conclude:
function checkPositive($x) {
return $x > 0;
}

Related

I misuderstand why doesn't work and how to fix it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My exercise is to place all the characters in alphabetical order first, then numbers, and finally all the other characters, each in the following 3 groups following the ASCII order.
It would be like
./ssap.php toto tutu 4234 "_hop A2l+ XXX" ## "1948372 AhAhAh"
AhAhAh
A2l+
toto
tutu
XXX
1948372
4234
##
_hop
You cannot do it with simple asort, replace it with uasort.
This is a hint for a possible approach, you can finish that by adding more conditions (see the comment inside):
uasort($ar, function($a, $b) {
if(ctype_alpha(substr($a, 0, 1)) && is_numeric(substr($b, 0, 1))) {
echo "comparing $a $b\n";
return -1;
}
if(ctype_alpha(substr($b, 0, 1)) && is_numeric(substr($a, 0, 1))) {
echo "comparingx $a $b\n";
return 1;
}
// add more code here to compare words with non-alphanumeric characters
// and other custom conditions if you find some issues
return ($a < $b) ? -1 : 1;
});

Php floating if statement Confused [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
function is_decimal( $it )
{
return is_numeric( $it ) && floor( $it ) != $it;
}
if (is_decimal == true){
echo "Decimal";
}
This gives me an error even though the int is not a decimal (float). Can someone help me. Thanks
It works fine for a decimal but not for an int.
Simple, when you call a function with a parameter you have to pass a parameter!
function is_decimal( $spaces )
{
return is_numeric( $spaces ) && floor( $spaces ) != $spaces;
}
$tst_var = 1.99
if (is_decimal($tst_var) == true){
echo "Error 3: Decimal causing Error as carpark can't have decimal spaces available.";
}
just use if(is_numeric( $spaces )){ ... } work for int, float even if they are represented by string

How does one correctly translate the Weight Watcher PointPlus formulas to code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Looking for the correct way to code the Weight Watcher PointsPlus formulas for "Food Values" and "Food with Alcohol and Sugar Alcohol" into Ruby and/or PHP.
Here is the link to the formula: http://en.wikipedia.org/wiki/Weight_Watchers#Formulas
Thanks in advance for the help!
Update:
As per the help from #mjayt below, I have the code below. My goal is to learn how to properly think, organize and solve formulas through code. I believe help correctly solving the two formulas will greatly help. Thanks again.
$protein = 8;
$carbs = 30;
$fat = 12;
$fiber = 8;
$pointsplus = round((($protein * 16) + ($carbs * 19) + ($fat * 45) - ($fiber * 14)) / 175, 0);
echo $pointsplus;
Okay, so I was bored... here is the first one. This should get you going
$protein = 8;
$carbs = 30;
$fat = 12;
$fiber = 8;
$pointsplus = round((($protein * 16) + ($carbs * 19) + ($fat * 45) - ($fiber * 14)) / 175, 0);
echo $pointsplus;

Get value from a percentage from a range of 2 numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example I have this range:
20 to 200
How can I programmatically get the value for a specific percentage, lets say 40% between those 2 numbers?
Simple example:
0 to 200
I want to get the value of 40%. Answer is 80.
Try this:
function percentage_from_range($from, $to, $percentage) {
$result = (($to - $from) / 100 * $percentage) + $from;
return $result;
}
e.g.
echo percentage_from_range(20, 200, 40); //92
$num1=20;
$num2=200;
$percent=40;
$total=$num2-$num1;
$result = (($total/100) * $percent)+$num1;
$lwb = 20;
$upb = 200;
$perc = 40;
$value = $lwb + ($upb - $lwb) * $perc / 100;
Takes perc/100 part of range, and start with the lowerbound.

How to generate an alphabetic incremental string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example
I'm looking for a function that keeps incrementing letters
Starts wit "aaaaaa" and goes to "aaaaab" and ends with "zzzzzz"
Use PHP's ability to increment characters Perl style
$char = 'aaaaaa';
$endChar = 'zzzzzz';
$endChar++;
while ($char != $endChar) {
echo $char++, PHP_EOL;
}
Just use increment operator
$start = "aaaaaa";
$end = "zzzzzz";
while($start ++ != $end) {
// print after every 10,000,000 million
(#$i ++ % 10000000 == 0) and print($start . PHP_EOL);
}
echo "Total  ", number_format($i, 0);
Output
aaaaab
avwyxl
brtxuv
cnqwsf
djnvpp
efkumz
fbhtkj
fxesht
gtbrfd
hoyqcn
ikvozx
jgsnxh
kcpmur
kymlsb
lujkpl
mqgjmv
nmdikf
oiahhp
pdxgez
pzufcj
qvrdzt
rrocxd
snlbun
tjiarx
ufezph
vbbymr
vwyxkb
wsvwhl
xosvev
ykpucf
zgmszp
Total 308,915,775

Categories