php判断⼩数点的长度_PHP:获取⼩数位数PHP:获取⼩数位数
有没有⼀种简单的⽅法来确定PHP中a(n)整数/双精度值的⼩数位数? (即,不使⽤explode)
Erwin asked 2020-07-28T21:50:58Z
17个解决⽅案
72 votes
$str = "1.23444";
print strlen(substr(strrchr($str, "."), 1));
ghostdog74 answered 2020-07-28T21:51:02Z
11 votes
您可以尝试将其强制转换为int,然后从您的数字中减去该数字,然后计算剩余的数字。
Allyn answered 2020-07-28T21:51:22Z
11 votes
function numberOfDecimals($value)
{
if ((int)$value == $value)
邮政银行余额查询{
return 0;
}
el if (! is_numeric($value))
{
// throw new Exception('numberOfDecimals: ' . $value . ' is not a number!');
辣椒牛肉return fal;
}
return strlen($value) - strrpos($value, '.') - 1;
}
/* test and proof */
function test($value)
{
printf("Testing [%s] : %d decimals\n", $value, numberOfDecimals($value));
}
foreach(array(1, 1.1, 1.22, 123.456, 0, 1.0, '1.0', 'not a number') as $value)
{
test($value);
}
输出:
Testing [1] : 0 decimals
Testing [1.1] : 1 decimals
燕窝早上吃好还是晚上吃好
Testing [1.22] : 2 decimals
Testing [123.456] : 3 decimals
Testing [0] : 0 decimals
Testing [1] : 0 decimals
Testing [1.0] : 0 decimals
Testing [not a number] : 0 decimals
Kris answered 2020-07-28T21:51:41Z
3 votes
我使⽤以下⽅法确定返回的值是否具有任何⼩数位数(实际的⼗进制值,⽽不仅仅是格式化为显⽰100.00之类的⼩数):if($mynum - floor($mynum)>0) {has decimals;} el {no decimals;}
skips answered 2020-07-28T21:52:01Z
2 votes
就像是:
$floatNum = "120.340304";
$length = strlen($floatNum);
$pos = strpos($floatNum, "."); // zero-bad counting.
$num_of_dec_places = ($length - $pos) - 1; // -1 to compensate for the zero-bad count in strpos()
>
这是程序上的,很笨拙,我不建议在⽣产代码中使⽤它。 但这应该可以帮助您⼊门。
David Thomas answered 2020-07-28T21:52:26Z
2 votes
test(0);
test(1);邶风击鼓
test(1.234567890);
test(-123.14);
test(1234567890);
test(12345.67890);
function test($f) {
echo "f = $f\n";
echo "i = ".getIntCount($f)."\n";
echo "d = ".getDecCount($f)."\n"; echo "\n";
}
function getIntCount($f) {
if ($f === 0) {
return 1;
} elif ($f < 0) {
return getIntCount(-$f);
} el {
return floor(log10(floor($f))) + 1;
}
}
function getDecCount($f) {
$num = 0;
while (true) {
if ((string)$f === (string)round($f)) { break;
}
if (is_infinite($f)) {
break;
}
$f *= 10;
$num++;
}
return $num;
}
输出:
f = 0
i = 1
d = 0
f = 1
i = 1
d = 0
f = 1.23456789
i = 1
d = 8
f = -123.14
i = 3
d = 2
f = 1234567890
i = 10
d = 0
f = 12345.6789
i = 5
d = 4
手工拼贴画
Sergey answered 2020-07-28T21:52:46Z
2 votes
我需要⼀种适⽤于各种数字格式的解决⽅案,并提出了以下算法:
// Count the number of decimal places
$current = $value - floor($value);
for ($decimals = 0; ceil($current); $decimals++) {
$current = ($value * pow(10, $decimals + 1)) - floor($value * pow(10, $decimals + 1)); }
每伴
// Count the total number of digits (includes decimal places)
$current = floor($value);
for ($digits = $decimals; $current; $digits++) {
$current = floor($current / 10);
}
结果:
input: 1
decimals: 0
digits: 1
input: 100
decimals: 0
digits: 3
input: 0.04
decimals: 2
digits: 2
input: 10.004
decimals: 3
海南岛有多大面积digits: 5
input: 10.0000001
decimals: 7
digits: 9
灵兰秘典论
input: 1.2000000992884E-10
decimals: 24
digits: 24
input: 1.2000000992884e6
decimals: 7
digits: 14
Jeremy Worboys answered 2020-07-28T21:53:10Z
2 votes
如果您希望可读性对其他开发⼈员有利,并且语⾔环境安全,请使⽤:function countDecimalPlacesUsingStrrpos($stringValue){ $locale_info = localeconv();
$pos = strrpos($stringValue, $locale_info['decimal_point']);
if ($pos !== fal) {
return strlen($stringValue) - ($pos + 1);
}
return 0;
}
见localeconv
Ian answered 2020-07-28T21:53:34Z
1 votes
$decnumber = strlen(strstr($yourstr,'.'))-1
Young answered 2020-07-28T21:53:49Z
1 votes
这个怎么样?:
$iDecimals = strlen($sFull%1);
Anton answered 2020-07-28T21:54:09Z
1 votes
这是⼀个考虑尾随零的函数: