mvccore/ext-tool-locale-floatparser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mvccore/ext-tool-locale-floatparser

Composer 安装命令:

composer require mvccore/ext-tool-locale-floatparser

包简介

MvcCore - Extension - Tool - Locale - FloatParser - parse float by automatic floating point detection or parse float value by `Intl` extension.

README 文档

README

Parse float by automatic floating point detection or parse float value by Intl extension.

Latest Stable Version License PHP Version

Installation

composer require mvccore/ext-tool-locale-floatparser

Usage

Non Intl Automatic Floating Point Detection

Non Intl parsing with automatic floating point detection has better results for unexpected user inputs like: '1.2 3' => 1.23 (float) '1.2 3 and 4' => 1.234 (float) '-1,234,567.89' => -1234567.89 (float) '-1.234.567,89' => -1234567.89 (float) '-1 234 567.89' => -1234567.89 (float) '-1 234 567,89' => -1234567.89 (float)

Example

$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 Parsing

Intl parser needs to set up language and locale more precisely to user input expectations, which is more error prone in very foreing languages and their locale conventions.

Mostly all languages use floating point character . or ,, but Intl library has sometimes not the same values for floating point char in specific locale as operation system has for specific locale, there is a little mess. That's why sometimes there is better to use non Intl floating point parsing.

Example

$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

统计信息

  • 总下载量: 1.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-09-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固