dsagzmosl32217092 2014-04-30 18:55

# 将浮点数分成数组

So I have a decimal (5,2) that I am trying to separate into an array.

So if the number is..

`123.45 = array([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )`

`23.45 = array([0] => 0 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )`

`3.45 = array([0] => 0 [1] => 0 [2] => 3 [3] => 4 [4] => 5 )`

I have a pretty large code to do this, and I wanted to know if anyone had a simpler way of putting each digit into this array because I feel like mine is pretty convoluted.

``````\$number = 123.35;
\$number_array = explode(".", \$number);
\$final_array = array(0);

if(!empty(@(string)\$number_array[0][0]) && !empty(@(string)\$number_array[0][1]) && !empty(@(string)\$number_array[0][2]))
{
\$final_array[0] = @(string)\$number_array[0][0];
\$final_array[1] = @(string)\$number_array[0][1];
\$final_array[2] = @(string)\$number_array[0][2];
}
elseif(!empty(@(string)\$number_array[0][0]) && !empty(@(string)\$number_array[0][1]) && empty(@(string)\$number_array[0][2]))
{
\$final_array[0] = 0;
\$final_array[1] = @(string)\$number_array[0][0];
\$final_array[2] = @(string)\$number_array[0][1];
}
elseif(!empty(@(string)\$number_array[0][0]) && empty(@(string)\$number_array[0][1]) && empty(@(string)\$number_array[0][2]))
{
\$final_array[0] = 0;
\$final_array[1] = 0;
\$final_array[2] = @(string)\$number_array[0][0];
}
else
{
\$final_array[0] = 0;
\$final_array[1] = 0;
\$final_array[2] = 0;
}

empty(@(string)\$number_array[1][0]) ? \$final_array[3] = 0 : \$final_array[3] = @(string)\$number_array[1][0];
empty(@(string)\$number_array[1][1]) ? \$final_array[4] = 0 : \$final_array[4] = @(string)\$number_array[1][1];
``````
• 写回答

#### 4条回答默认 最新

• dthy81285 2014-04-30 19:00
关注
``````\$number = str_replace('.', '', (string) 13.35);
var_dump(str_split(\$number));
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 ios 新安装app收不到fcm推送
• ¥15 有没有实力的写手？有过成品的优先
• ¥15 图像信息库的建立与识别
• ¥15 韩国网站购物，KG支付的支付回调如何解决
• ¥15 workstation导入ovf文件，报错，怎么解决呢？
• ¥15 关于#c语言#的问题：构成555单稳态触发器，采用LED指示灯延时时间，对延时时间进行测量并显示（如楼道声控延时灯）需要Proteus仿真图和C语言代码
• ¥15 workstation加载centos进入emergency模式，查看日志报警如图，怎样解决呢？
• ¥50 如何用单纯形法寻优不能精准找不到给定的参数，并联机构误差识别，给定误差有7个？matlab
• ¥15 workstation加载centos进入emergency模式，查看日志报警如图，没有XFS,怎样解决呢？
• ¥15 应用商店如何检测在架应用内容是否违规？