mvccore/ext-tool-locale-floatparser

MvcCore - 扩展 - 工具 - 区域 - 浮点解析 - 通过自动浮点检测或通过 `Intl` 扩展解析浮点值。

v5.2.0 2024-02-19 15:42 UTC

This package is auto-updated.

Last update: 2024-09-19 17:09:09 UTC


README

通过自动浮点检测解析浮点值或通过 Intl 扩展解析浮点值。

Latest Stable Version License PHP Version

安装

composer require mvccore/ext-tool-locale-floatparser

使用

Intl 自动浮点检测

Intl 解析使用自动浮点检测对于意外用户输入有更好的结果,例如:'1.2 3' => 1.23 (浮点数) '1.2 3 and 4' => 1.234 (浮点数) '-1,234,567.89' => -1234567.89 (浮点数) '-1.234.567,89' => -1234567.89 (浮点数) '-1 234 567.89' => -1234567.89 (浮点数) '-1 234 567,89' => -1234567.89 (浮点数)

示例

$autoParser = \MvcCore\Ext\Tools\Locales\FloatParser::CreateInstance(
	'en',	// international language code, lowercase 
	'US', 	// international country code, uppercase
	FALSE	// FALSE (by default) to prefer automatic floating point detection
);
var_dump($autoParser->Parse('-1,234,567.89'));	// -1234567.89	(float)
var_dump($autoParser->Parse('-1.234.567,89'));	// -1234567.89	(float)
var_dump($autoParser->Parse('-1 234 567.89'));	// -1234567.89	(float)
var_dump($autoParser->Parse('-1 234 567,89'));	// -1234567.89	(float)
var_dump($autoParser->Parse('1.2 3 and 4'));	// 1.234		(float)
var_dump($autoParser->Parse('1.2 3'));			// 1.23			(float)
// rest is the same as `Intl` floating point parser bellow:
var_dump($autoParser->Parse('1.8e308'));		// INF			(float)
var_dump($autoParser->Parse('1.79e308'));		// 1.79E+308	(float)
var_dump($autoParser->Parse('21474836470'));	// 21474836470	(float)
var_dump($autoParser->Parse('123'));			// 123.0		(float)
var_dump($autoParser->Parse('-3.14'));			// -3.14		(float)
var_dump($autoParser->Parse('1.2e3'));			// 1200			(float)
var_dump($autoParser->Parse('7E-10'));			// 7.0E-10		(float)
var_dump($autoParser->Parse('bob-1.3e3'));		// -1300		(float)
var_dump($autoParser->Parse('nothing'));		// NULL
var_dump($autoParser->Parse(TRUE));				// NULL
var_dump($autoParser->Parse([]));				// NULL
var_dump($autoParser->Parse(new \stdClass));	// NULL

Intl 解析

Intl 解析器需要更精确地设置语言和区域设置以符合用户输入的预期,这在非常外语和它们的区域约定中更容易出错。

几乎所有语言都使用浮点字符 .,,但 Intl 库有时在特定区域中浮点字符的值与操作系统在特定区域中的值不同,这有些混乱。这就是为什么有时使用非 Intl 浮点解析会更好。

示例

$intlParser = \MvcCore\Ext\Tools\Locales\FloatParser::CreateInstance(
	'en',	// international language code, lowercase 
	'US', 	// international country code, uppercase
	TRUE	// TRUE to prefer `Intl` extension parsing
);
var_dump($intlParser->Parse('-1,234,567.89'));	// -1234567.89	(float)
var_dump($intlParser->Parse('-1 234 567,89'));	// -1234567		(float)
var_dump($intlParser->Parse('-1 234 567.89'));	// -1234567.89	(float)
var_dump($intlParser->Parse('-1 234 567,89'));	// -1234567		(float)
var_dump($intlParser->Parse('1.2 3 and 4'));	// 1.2			(float)
var_dump($intlParser->Parse('1.2 3'));			// 1.2			(float)
// rest is the same as non `Intl` floating point parser above:
var_dump($autoParser->Parse('1.8e308'));		// INF			(float)
var_dump($autoParser->Parse('1.79e308'));		// 1.79E+308	(float)
var_dump($autoParser->Parse('21474836470'));	// 21474836470	(float)
var_dump($intlParser->Parse('123'));			// 123.0		(float)
var_dump($intlParser->Parse('-3.14'));			// -3.14		(float)
var_dump($intlParser->Parse('1.2e3'));			// 1200			(float)
var_dump($intlParser->Parse('7E-10'));			// 7.0E-10		(float)
var_dump($intlParser->Parse('bob-1.3e3'));		// -1300		(float)
var_dump($intlParser->Parse('nothing'));		// NULL
var_dump($intlParser->Parse(TRUE));				// NULL
var_dump($intlParser->Parse([]));				// NULL
var_dump($intlParser->Parse(new \stdClass));	// NULL