在十进制值之间循环 [英] Cycling through decimal values

查看:82
本文介绍了在十进制值之间循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为PHP项目的一部分,我需要返回任意间隔中包含的每个值的正弦值.此外,我还需要能够设置函数的作用域",即我需要循环多少个小数位.

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.

例如:包含1到3间隔的小数点后1位: 1,1.1,1.2,... 2.8,2.9,3

Eg: 1 decimal place for interval 1 to 3 included: 1, 1.1, 1.2, ... 2.8, 2.9, 3

在相同时间间隔内保留2个小数位

2 decimal places for the same interval

1、1.01、1.02 ... 2.98、2.99、3

1, 1.01, 1.02 ... 2.98, 2.99, 3

等等...我尝试使用"for"循环来执行此操作,但是它只会考虑自然数.

Etc... I tried doing it with the "for" cycle, but it would only consider natural numbers.

建议?

推荐答案

您可以根据需要调整以下代码:

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";
}

输出:

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

这篇关于在十进制值之间循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆