dongliping003116
2016-12-01 09:35 阅读 45
已采纳

将4位数整数转换为十进制数

I am trying to convert a 4 digit integer like 1999 to 19.99.

I have looked at functions like money_format() but this seems to not be what I am looking for. Here is a function I wrote but there must be pre-existing method to do this?

function numberToDecimal((int)$num) {
    if(!preg_match('/^\d{4}$/', $num) return;
    $parsed = (string)$num;
    $i      = 0;
    $ret    = $parsed[$i++] . $parsed[$i++] . '.';
    $ret   .= $parsed[$i++] . $parsed[$i];
    return $ret;
}

Thank-you in advance.
See the code working over at corresponding to the answer 3v4l.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douzhi9939 douzhi9939 2016-12-01 09:43

    What about the following:

    $money = 1999 ;
    echo number_format(($money /100), 2, '.', ' ');
    // Will output: 19.99
    
    点赞 评论 复制链接分享

相关推荐