如何通过php找到给定十进制值的位值? [英] How to find the place value for the given decimal value through php?

查看:54
本文介绍了如何通过php找到给定十进制值的位值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 php 不太熟悉,但我知道我们可以通过 php 找到给定数字的位值.例如,如果输入是 23.56,它应该回显 2 - 十、3 - 一个、5 - 百分之一、6 - 千分之一.

I'm not so familiar with php, but i know we could find the place value of a given number through php. For example if the input is 23.56 it should echo 2 - Tens, 3 - Ones, 5 - Hundredths, 6 - Thousandths.

任何想法将不胜感激.:) 请帮忙.

Any idea would be appreciated. :) please help.

推荐答案

尝试

$str = '23.56';
$strdiv = explode('.', $str);
$before = array('Tens', 'Ones');
$after = array('Hundredths', 'Thousandths');
$counter = 0;
foreach($strdiv as $v) {
  for($i=0; $i<strlen($v); $i++) {
     if(!empty($v)) {
       if($counter == 0) {
         $newarr[] = substr($v,$i, 1).' - '.$before[$i];
       }
       if($counter == 1) {
         $newarr[] = substr($v,$i, 1).' - '.$after[$i];
       }
     }
  }
  $counter++;
}
echo implode(', ',$newarr); //2 - Tens, 3 - Ones, 5 - Hundredths, 6 - Thousandths 

这篇关于如何通过php找到给定十进制值的位值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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