Cycling through decimal values
As part of a PHP project, I have the need of returning the sine value of each value included in an arbitrary interval. Moreover, I also need to be able to set the "scope" of the function, that is, how many decimal places I need to cycle.
Eg: 1 decimal place for interval 1 to 3 included: 1, 1.1, 1.2, ... 2.8, 2.9, 3
2 decimal places for the same interval
1, 1.01, 1.02 ... 2.98, 2.99, 3
Etc... I tried doing开发者_StackOverflow社区 it with the "for" cycle, but it would only consider natural numbers.
Suggestions?
You can tweak the code below code to suit your needs:
$start = 1;
$end = 3;
$place = 1;
$step = 1 / pow(10, $place);
for($i = $start; $i <= $end; $i = round($i + $step, $place))
{
echo $i . "\n";
}
Output:
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3
精彩评论