我有数百个数字:
struct vertice {
int accuracy;
double x, y, z;
std::vector<vertice*> linkedVertices;
vertice(double x, double y, double z, std::vector<vertice*> linkedVertices) {
this->x = x;
this->y = y;
this->z = z;
this->linkedVertices = linkedVertices;
}
vertice(double x, double y, double z, int accuracy) {
this->x = x;
this->y = y;
this->z = z;
this->accuracy = accuracy;
}
};
struct shape {
double center;
std::vector<vertice> vertices;
shape(double center, std::vector<vertice> vertices) {
this->center = center;
this->vertices = vertices;
}
};
我需要检查数字1在这些数字中的位置,所以基本
0.00100000
0.01000000
0.01000000
1.00000000
0.00001000
0.00000100
我尝试了1.00000000 = 1
0.10000000 = 2
0.01000000 = 3
功能,但它有时打印的数字如1.E-6或类似的东西,我需要确切位置的数字1。
非常感谢您的帮助。
答案 0 :(得分:0)
看起来我找到了一个解决方案,使用strpos()
功能完美运行!
Basicly:
strpos(0.01000000, 1)
答案 1 :(得分:0)
我不会过分依赖你在答案中发布的方法。请改用以下功能:
function index_of_one($dec)
{
// maximum precision is 15
$str = str_replace('.','',sprintf('%.15f', $dec));
$pos = strpos($str, '1');
if ($pos === false) {
return -1;
}
return ($pos + 1);
}
示例:
$dec1 = 1.00000000;
$dec2 = 0.10000000;
$dec3 = 0.00010000;
echo index_of_one($dec1); // 1
echo index_of_one($dec2); // 2
echo index_of_one($dec3); // 5
访问this link进行测试。
答案 2 :(得分:0)
我说使用正则表达式是合适的,当微优化不是最高优先级时,它会减少总函数调用,并且当它提供直接的,期望的结果时。
考虑到您的案例和示例输入,Sub newapproach()
Set ws1 = Workbooks("A.xlsx").Worksheets(1)
Workbooks("A.xlsx").Activate
lastRow11 = Range("I" & Rows.Count).End(xlUp).Row
For i = lastRow11 To 1 Step -1
If i <> "" Then
Value = Workbooks("A.xlsx").Worksheets(1).Cells(i, "I").Value
MsgBox (Value)
End If
Next i
End Sub
只需一步即可完成所有操作。它匹配每个单独的非点字符,后面跟着preg_match_all('/[^.](?=[^1]*1)|1.*/',$number)
OR从1
到字符串的末尾。
1
的返回值是匹配数,因此它可以提供您要求的 - 计数。
实际上,点数不计算,也不是第一个preg_match_all()
之后的任何字符。如果找不到1
,则计数为1
(不是其他答案建议的-1)。
以下是一系列测试(Demo)的演示:
0
输出:
$numbers=['0.00100000','0.10000000','0.01000000','1.00000000','0.00001000','0.00000100','0.00000000'];
foreach($numbers as $number){
echo "$number = ",preg_match_all('/[^.](?=[^1]*1)|1.*/',$number),"\n";
}