简体   繁体   English

PHP:如何检查一个数字是否有两个以上的小数

[英]PHP: How to check if a number has more than two decimals

I'm trying to pick out numbers with more than two decimals (more than two digits after the decimal separator).我试图挑出两位以上小数的数字(小数点分隔符后两位以上)。 I cant't figure out why this doesn't work:我不明白为什么这不起作用:

if ($num * 100 != floor($num * 100)) {
  echo "The number you entered has more than two decimals";
}

Why is the number 32.45 picked out, while 32.44 isn't ?为什么数字32.45 被选中,而 32.44 不是

You could use a regex:您可以使用正则表达式:

$number = 1.12; //Don't match
$number = 1.123; //Match
$number = 1.1234; //Match
$number = 1.123; //Match

if (preg_match('/\.\d{3,}/', $number)) {
    # Successful match
} else {
    # Match attempt failed
}

You can use a regex to figure out if it has more than 2 decimals:您可以使用正则表达式来确定它是否有超过 2 个小数:

<?php
    function doesNumberHaveMoreThan2Decimals($number) {
        return (preg_match('/\.[0-9]{2,}[1-9][0-9]*$/', (string)$number) > 0);
    }

    $numbers = array(123.456, 123.450, '123.450', 123.45000001, 123, 123.4);

    foreach ($numbers as $number) {
        echo $number . ': ' . (doesNumberHaveMoreThan2Decimals($number) ? 'Y' : 'N') . PHP_EOL;
    }
?>

Output:输出:

123.456:      Y
123.45:       N
123.450:      N
123.45000001: Y
123:          N
123.4:        N

DEMO演示

Regex autopsy ( /\\.[0-9]{2,}[1-9][0-9]*$/ ):正则表达式尸检( /\\.[0-9]{2,}[1-9][0-9]*$/ ):

  • \\. - a literal . - 一个字面意思. character特点
  • [0-9]{2,} - Digits from 0 to 9 matched 2 or more times [0-9]{2,} - 0 到 9 的数字匹配了 2 次或更多次
  • [1-9] - A digit between 1 and 9 matched a single time (to make sure we ignore trailing zeroes) [1-9] - 1 到 9 之间的数字匹配一次(以确保我们忽略尾随零)
  • [0-9]* - A digit between 0 and 9 matched 0 to infinity times (to make sure that we allow 123.4510 even though it ends with 0 ). [0-9]* - 0 到 9 之间的数字匹配 0 到无穷大(以确保我们允许123.4510即使它以0结尾)。
  • $ - The string MUST end here - nothing else can be between our last match and the end of the string $ - 字符串必须在此处结束 - 在我们的最后一次匹配和字符串结尾之间不能有任何其他内容

You could use the following function (works with negative numbers, too):您可以使用以下函数(也适用于负数):

function decimalCheck($num) {
    $decimals = ( (int) $num != $num ) ? (strlen($num) - strpos($num, '.')) - 1 : 0;
    return $decimals >= 2;
}

Test cases:测试用例:

$numbers = array(
    32.45,
    32.44,
    123.21,
    21.5454,
    1.545400,
    2.201054,
    0.05445,
    32,
    12.0545400,
    12.64564,
    -454.44,
    -0.5454
);
foreach ($numbers as $number) {
    echo $number. "\t : \t";
    echo (decimalCheck($number)) ? 'true' : 'false';
    echo "<br/>";
}

Output:输出:

32.45    :  true
32.44    :  true
123.21   :  true
21.5454  :  true
1.5454   :  true
2.201054 :  true
0.05445  :  true
32       :  false
12.05454 :  true
12.64564 :  true
-454.44  :  true
-0.5454  :  true

Demo.演示。

I know, its an old question, but why not just do:我知道,这是一个老问题,但为什么不这样做:

function onlyDecimals($number, $maxDecimalPlaces = 2) {
   return $amount == number_format($amount, $maxDecimalPlaces, ".", "");
}

See example and tests: http://sandbox.onlinephpfunctions.com/code/e68143a9ed0b6dfcad9ab294c44fa7e802c39dd0查看示例和测试: http : //sandbox.onlinephpfunctions.com/code/e68143a9ed0b6dfcad9ab294c44fa7e802c39dd0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM