函数文档

wp_fuzzy_number_match()

💡 云策文档标注

概述

wp_fuzzy_number_match() 函数用于检查两个数字是否近似相等,通过比较差值是否在指定精度范围内。

关键要点

  • 函数接受三个参数:$expected(期望值,必需)、$actual(实际值,必需)和 $precision(允许的误差范围,可选,默认值为1)。
  • 返回布尔值,表示数字是否在指定精度内匹配。
  • 类似于 round() 函数,但精度控制更精细。
  • 在 WordPress 5.3.0 版本中引入。

代码示例

function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
    return abs( (float) $expected - (float) $actual ) <= $precision;
}

相关函数

  • wp_image_matches_ratio():用于测试两个图像的宽高比是否匹配。
  • image_resize_dimensions():检索计算出的调整尺寸,用于 WP_Image_Editor。

📄 原文内容

Checks if two numbers are nearly the same.

Description

This is similar to using round() but the precision is more fine-grained.

Parameters

$expectedint|floatrequired
The expected value.
$actualint|floatrequired
The actual number.
$precisionint|floatoptional
The allowed variation.

Default:1

Return

bool Whether the numbers match within the specified precision.

Source

function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
	return abs( (float) $expected - (float) $actual ) <= $precision;
}

Changelog

Version Description
5.3.0 Introduced.