Created
November 20, 2020 03:24
-
-
Save tamakiii/2976b56497bc4313be3d5dd4a8533e03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// See also: https://github.com/symfony/symfony/blob/3c0cfbcb8cf322666d13bbde4e7cd469c360e6c1/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php | |
ini_set('xdebug.var_display_max_children', -1); | |
ini_set('xdebug.var_display_max_data', -1); | |
ini_set('xdebug.var_display_max_depth', -1); | |
require __DIR__ . '/NumberFormatterTest.php'; | |
// like: en_US, ja_JP | |
$locale = $argv[1] ?? (Locale::getDefault() ?: current(array_filter(array_map( | |
function ($lang) { | |
return explode('.', $lang)[0]; | |
}, | |
[getenv('LANG'), getenv('LANGUAGE'), getenv('LC_TYPE'), getenv('LC_ALL')] | |
)))); | |
Locale::setDefault($locale); | |
$results = []; | |
foreach (NumberFormatterTest::STYLES as $name => $style) { | |
$formatter = new NumberFormatter($locale, $style); | |
$values = [ | |
$formatter->parse(0.00), | |
$formatter->parse(0.01), | |
$formatter->parse(100), | |
$formatter->parse(1000), | |
$formatter->parse(1000.00), | |
$formatter->parse(1000.01), | |
$formatter->parse(1000.001), | |
$formatter->parse(1000.0001), | |
$formatter->parse('0.00'), | |
$formatter->parse('0.01'), | |
$formatter->parse('100'), | |
$formatter->parse('1000'), | |
$formatter->parse('1000.00'), | |
$formatter->parse('1000.01'), | |
$formatter->parse('1000.001'), | |
$formatter->parse('1000.0001'), | |
]; | |
$results[] = [ | |
'style' => $name, | |
'pattern' => $formatter->getPattern(), | |
'rounding' => array_flip(NumberFormatterTest::ATTRIBUTES['rounding'])[$formatter->getAttribute(NumberFormatter::ROUNDING_MODE)] ?? null, | |
'pad_position' => array_flip(NumberFormatterTest::ATTRIBUTES['pad_position'])[$formatter->getAttribute(NumberFormatter::PADDING_POSITION)] ?? null, | |
'text' => array_map( | |
function($attribute) use ($formatter) { | |
return $formatter->getTextAttribute($attribute); | |
}, | |
NumberFormatterTest::ATTRIBUTES['text'] | |
), | |
'symbols' => array_map( | |
function($attribute) use ($formatter) { | |
return $formatter->getSymbol($attribute); | |
}, | |
NumberFormatterTest::ATTRIBUTES['symbol'] | |
), | |
'format' => array_map( | |
function($value) use ($formatter) { | |
return $formatter->format($value); | |
}, | |
$values | |
), | |
'parse' => array_map( | |
function($value) use ($formatter) { | |
return $formatter->parse($value); | |
}, | |
$values | |
), | |
]; | |
} | |
var_dump($results); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NumberFormatterTest | |
{ | |
const STYLES = [ | |
'NumberFormatter::PATTERN_DECIMAL' => NumberFormatter::PATTERN_DECIMAL, | |
//Decimal format defined by pattern | |
'NumberFormatter::DECIMAL' => NumberFormatter::DECIMAL, | |
// Decimal format | |
'NumberFormatter::CURRENCY' => NumberFormatter::CURRENCY, | |
// Currency format | |
'NumberFormatter::PERCENT' => NumberFormatter::PERCENT, | |
// Percent format | |
'NumberFormatter::SCIENTIFIC' => NumberFormatter::SCIENTIFIC, | |
// Scientific format | |
'NumberFormatter::SPELLOUT' => NumberFormatter::SPELLOUT, | |
// Spellout rule-based format | |
'NumberFormatter::ORDINAL' => NumberFormatter::ORDINAL, | |
// Ordinal rule-based format | |
'NumberFormatter::DURATION' => NumberFormatter::DURATION, | |
// Duration rule-based format | |
// 'NumberFormatter::PATTERN_RULEBASED' => NumberFormatter::PATTERN_RULEBASED, | |
// // Rule-based format defined by pattern | |
'NumberFormatter::CURRENCY_ACCOUNTING' => NumberFormatter::CURRENCY_ACCOUNTING, | |
// Currency format for accounting, e.g., ($3.00) for negative currency amount instead of -$3.00. Available as of PHP 7.4.1 and ICU 53. | |
'NumberFormatter::DEFAULT_STYLE' => NumberFormatter::DEFAULT_STYLE, | |
// Default format for the locale | |
'NumberFormatter::IGNORE' => NumberFormatter::IGNORE, | |
// Alias for PATTERN_DECIMAL | |
]; | |
const ATTRIBUTES = [ | |
'parse' => [ | |
'NumberFormatter::TYPE_DEFAULT' => NumberFormatter::TYPE_DEFAULT, | |
// Derive the type from variable type | |
'NumberFormatter::TYPE_INT32' => NumberFormatter::TYPE_INT32, | |
// Format/parse as 32-bit integer | |
'NumberFormatter::TYPE_INT64' => NumberFormatter::TYPE_INT64, | |
// Format/parse as 64-bit integer | |
'NumberFormatter::TYPE_DOUBLE' => NumberFormatter::TYPE_DOUBLE, | |
// Format/parse as floating point value | |
'NumberFormatter::TYPE_CURRENCY' => NumberFormatter::TYPE_CURRENCY, | |
// Format/parse as currency value | |
], | |
'numer_format' => [ | |
'NumberFormatter::PARSE_INT_ONLY' => NumberFormatter::PARSE_INT_ONLY, | |
// Parse integers only. | |
'NumberFormatter::GROUPING_USED' => NumberFormatter::GROUPING_USED, | |
// Use grouping separator. | |
'NumberFormatter::DECIMAL_ALWAYS_SHOWN' => NumberFormatter::DECIMAL_ALWAYS_SHOWN, | |
// Always show decimal point. | |
'NumberFormatter::MAX_INTEGER_DIGITS' => NumberFormatter::MAX_INTEGER_DIGITS, | |
// Maximum integer digits. | |
'NumberFormatter::MIN_INTEGER_DIGITS' => NumberFormatter::MIN_INTEGER_DIGITS, | |
// Minimum integer digits. | |
'NumberFormatter::INTEGER_DIGITS' => NumberFormatter::INTEGER_DIGITS, | |
// Integer digits. | |
'NumberFormatter::MAX_FRACTION_DIGITS' => NumberFormatter::MAX_FRACTION_DIGITS, | |
// Maximum fraction digits. | |
'NumberFormatter::MIN_FRACTION_DIGITS' => NumberFormatter::MIN_FRACTION_DIGITS, | |
// Minimum fraction digits. | |
'NumberFormatter::FRACTION_DIGITS' => NumberFormatter::FRACTION_DIGITS, | |
// Fraction digits. | |
'NumberFormatter::MULTIPLIER' => NumberFormatter::MULTIPLIER, | |
// Multiplier. | |
'NumberFormatter::GROUPING_SIZE' => NumberFormatter::GROUPING_SIZE, | |
// Grouping size. | |
'NumberFormatter::ROUNDING_MODE' => NumberFormatter::ROUNDING_MODE, | |
// Rounding Mode. | |
'NumberFormatter::ROUNDING_INCREMENT' => NumberFormatter::ROUNDING_INCREMENT, | |
// Rounding increment. | |
'NumberFormatter::FORMAT_WIDTH' => NumberFormatter::FORMAT_WIDTH, | |
// The width to which the output of format() is padded. | |
'NumberFormatter::PADDING_POSITION' => NumberFormatter::PADDING_POSITION, | |
// The position at which padding will take place. See pad position constants for possible argument values. | |
'NumberFormatter::SECONDARY_GROUPING_SIZE' => NumberFormatter::SECONDARY_GROUPING_SIZE, | |
// Secondary grouping size. | |
'NumberFormatter::SIGNIFICANT_DIGITS_USED' => NumberFormatter::SIGNIFICANT_DIGITS_USED, | |
// Use significant digits. | |
'NumberFormatter::MIN_SIGNIFICANT_DIGITS' => NumberFormatter::MIN_SIGNIFICANT_DIGITS, | |
// Minimum significant digits. | |
'NumberFormatter::MAX_SIGNIFICANT_DIGITS' => NumberFormatter::MAX_SIGNIFICANT_DIGITS, | |
// Maximum significant digits. | |
'NumberFormatter::LENIENT_PARSE' => NumberFormatter::LENIENT_PARSE, | |
// Lenient parse mode used by rule-based formats. | |
], | |
'text' => [ | |
'NumberFormatter::POSITIVE_PREFIX' => NumberFormatter::POSITIVE_PREFIX, | |
// Positive prefix. | |
'NumberFormatter::POSITIVE_SUFFIX' => NumberFormatter::POSITIVE_SUFFIX, | |
// Positive suffix. | |
'NumberFormatter::NEGATIVE_PREFIX' => NumberFormatter::NEGATIVE_PREFIX, | |
// Negative prefix. | |
'NumberFormatter::NEGATIVE_SUFFIX' => NumberFormatter::NEGATIVE_SUFFIX, | |
// Negative suffix. | |
'NumberFormatter::PADDING_CHARACTER' => NumberFormatter::PADDING_CHARACTER, | |
// The character used to pad to the format width. | |
'NumberFormatter::CURRENCY_CODE' => NumberFormatter::CURRENCY_CODE, | |
// The ISO currency code. | |
'NumberFormatter::DEFAULT_RULESET' => NumberFormatter::DEFAULT_RULESET, | |
// The default rule set. This is only available with rule-based formatters. | |
'NumberFormatter::PUBLIC_RULESETS' => NumberFormatter::PUBLIC_RULESETS, | |
// The public rule sets. This is only available with rule-based formatters. This is a read-only attribute. The public rulesets are returned as a single string, with each ruleset name delimited by ';' (semicolon). | |
], | |
'symbol' => [ | |
'NumberFormatter::DECIMAL_SEPARATOR_SYMBOL' => NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, | |
// The decimal separator. | |
'NumberFormatter::GROUPING_SEPARATOR_SYMBOL' => NumberFormatter::GROUPING_SEPARATOR_SYMBOL, | |
// The grouping separator. | |
'NumberFormatter::PATTERN_SEPARATOR_SYMBOL' => NumberFormatter::PATTERN_SEPARATOR_SYMBOL, | |
// The pattern separator. | |
'NumberFormatter::PERCENT_SYMBOL' => NumberFormatter::PERCENT_SYMBOL, | |
// The percent sign. | |
'NumberFormatter::ZERO_DIGIT_SYMBOL' => NumberFormatter::ZERO_DIGIT_SYMBOL, | |
// Zero. | |
'NumberFormatter::DIGIT_SYMBOL' => NumberFormatter::DIGIT_SYMBOL, | |
// Character representing a digit in the pattern. | |
'NumberFormatter::MINUS_SIGN_SYMBOL' => NumberFormatter::MINUS_SIGN_SYMBOL, | |
// The minus sign. | |
'NumberFormatter::PLUS_SIGN_SYMBOL' => NumberFormatter::PLUS_SIGN_SYMBOL, | |
// The plus sign. | |
'NumberFormatter::CURRENCY_SYMBOL' => NumberFormatter::CURRENCY_SYMBOL, | |
// The currency symbol. | |
'NumberFormatter::INTL_CURRENCY_SYMBOL' => NumberFormatter::INTL_CURRENCY_SYMBOL, | |
// The international currency symbol. | |
'NumberFormatter::MONETARY_SEPARATOR_SYMBOL' => NumberFormatter::MONETARY_SEPARATOR_SYMBOL, | |
// The monetary separator. | |
'NumberFormatter::EXPONENTIAL_SYMBOL' => NumberFormatter::EXPONENTIAL_SYMBOL, | |
// The exponential symbol. | |
'NumberFormatter::PERMILL_SYMBOL' => NumberFormatter::PERMILL_SYMBOL, | |
// Per mill symbol. | |
'NumberFormatter::PAD_ESCAPE_SYMBOL' => NumberFormatter::PAD_ESCAPE_SYMBOL, | |
// Escape padding character. | |
'NumberFormatter::INFINITY_SYMBOL' => NumberFormatter::INFINITY_SYMBOL, | |
// Infinity symbol. | |
'NumberFormatter::NAN_SYMBOL' => NumberFormatter::NAN_SYMBOL, | |
// Not-a-number symbol. | |
'NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL' => NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL, | |
// Significant digit symbol. | |
'NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL' => NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, | |
// The monetary grouping separator. | |
], | |
'rounding' => [ | |
'NumberFormatter::ROUNDING_MODE' => NumberFormatter::ROUNDING_MODE, | |
// attribute. | |
'NumberFormatter::ROUND_CEILING' => NumberFormatter::ROUND_CEILING, | |
// Rounding mode to round towards positive infinity. | |
'NumberFormatter::ROUND_DOWN' => NumberFormatter::ROUND_DOWN, | |
// Rounding mode to round towards zero. | |
'NumberFormatter::ROUND_FLOOR' => NumberFormatter::ROUND_FLOOR, | |
// Rounding mode to round towards negative infinity. | |
'NumberFormatter::ROUND_HALFDOWN' => NumberFormatter::ROUND_HALFDOWN, | |
// Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. | |
'NumberFormatter::ROUND_HALFEVEN' => NumberFormatter::ROUND_HALFEVEN, | |
// Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. | |
'NumberFormatter::ROUND_HALFUP' => NumberFormatter::ROUND_HALFUP, | |
// Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. | |
'NumberFormatter::ROUND_UP' => NumberFormatter::ROUND_UP, | |
// Rounding mode to round away from zero. | |
], | |
'pad_position' => [ | |
'NumberFormatter::PADDING_POSITION' => NumberFormatter::PADDING_POSITION, | |
// attribute. | |
'NumberFormatter::PAD_AFTER_PREFIX '=> NumberFormatter::PAD_AFTER_PREFIX, | |
// Pad characters inserted after the prefix. | |
'NumberFormatter::PAD_AFTER_SUFFIX' => NumberFormatter::PAD_AFTER_SUFFIX, | |
// Pad characters inserted after the suffix. | |
'NumberFormatter::PAD_BEFORE_PREFIX' => NumberFormatter::PAD_BEFORE_PREFIX, | |
// Pad characters inserted before the prefix. | |
'NumberFormatter::PAD_BEFORE_SUFFIX' => NumberFormatter::PAD_BEFORE_SUFFIX, | |
// Pad characters inserted before the suffix. | |
], | |
]; | |
} |
Author
tamakiii
commented
Nov 20, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment