如何在php中计算包含零的数字位数

栏目: PHP · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/17037255/how-to-count-number-of-digit-included-zero-in-php

我正在计算 PHP 中的数字位数.我只想计算数据库的数字值.在第一个数字有零意味着,它不会采取

作为计数的数字.

例如:

12  ==number of count value is 2
122 ==number of count value is 3

我可以通过function.here我的函数实现这一点.

function count_digit($number)
{
    return strlen((string) $number);
}

$number = 12312;
echo count_digit($number); // 5

但是我需要为该数字添加零$num = 0012312; (零填充).

012 == number of count value is 3
0133 == number of count value is 4

让我知道如何解决它.

如果您希望计算前导零,则应将其指定为字符串而不是数字.

然后,尝试计算字符数.这一次,它将包括零.无需在函数内部输入强制转换.

那么,现在您的代码将如下所示:

function count_digit($number) {
  return strlen($number);
}

//function call
$num = "number here";
$number_of_digits = count_digit($num); //this is call :)
echo $number_of_digits;
//prints 5

翻译自:https://stackoverflow.com/questions/17037255/how-to-count-number-of-digit-included-zero-in-php


以上所述就是小编给大家介绍的《如何在php中计算包含零的数字位数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Letting Go of the Words

Letting Go of the Words

Janice (Ginny) Redish / Morgan Kaufmann / 2007-06-11 / USD 49.95

"Redish has done her homework and created a thorough overview of the issues in writing for the Web. Ironically, I must recommend that you read her every word so that you can find out why your customer......一起来看看 《Letting Go of the Words》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码