コード例 #1
0
function ut_main()
{
    $fmt = ut_nfmt_create('en_US', NumberFormatter::DECIMAL);
    $number = 1234567.8912345679;
    $str_res = ut_nfmt_format($fmt, $number, NumberFormatter::TYPE_INT32) . "\n";
    return $str_res;
}
コード例 #2
0
function ut_main()
{
    $res_str = '';
    $test_value = 12345.123456;
    $fmt = ut_nfmt_create("en_US", NumberFormatter::PATTERN_DECIMAL);
    // Get default patten.
    $res_str .= "Default pattern: '" . ut_nfmt_get_pattern($fmt) . "'\n";
    $res_str .= "Formatting result: " . ut_nfmt_format($fmt, $test_value) . "\n";
    // Set a new pattern.
    $res = ut_nfmt_set_pattern($fmt, "0.0");
    if ($res === false) {
        $res_str .= ut_nfmt_get_error_message($fmt) . " (" . ut_nfmt_get_error_code($fmt) . ")\n";
    }
    // Check if the pattern has been changed.
    $res = ut_nfmt_get_pattern($fmt);
    if ($res === false) {
        $res_str .= ut_nfmt_get_error_message($fmt) . " (" . ut_nfmt_get_error_code($fmt) . ")\n";
    }
    $res_str .= "New pattern: '" . ut_nfmt_get_pattern($fmt) . "'\n";
    $res_str .= "Formatted number: " . ut_nfmt_format($fmt, $test_value) . "\n";
    ut_nfmt_set_pattern($fmt, str_repeat('@', 200));
    $res_str .= "New pattern: '" . ut_nfmt_get_pattern($fmt) . "'\n";
    $res_str .= "Formatted number: " . ut_nfmt_format($fmt, $test_value) . "\n";
    return $res_str;
}
コード例 #3
0
function ut_main()
{
    $locales = array('en_UK' => 'GBP', 'en_US' => 'USD', 'ru' => 'RUR', 'uk' => 'UAH', 'en' => 'UAH');
    $res_str = '';
    $number = 1234567.89;
    foreach ($locales as $locale => $currency) {
        $fmt = ut_nfmt_create($locale, NumberFormatter::CURRENCY);
        $res_str .= "{$locale}: " . var_export(ut_nfmt_format_currency($fmt, $number, $currency), true) . "\n";
    }
    return $res_str;
}
コード例 #4
0
ファイル: formatter_get_error.php プロジェクト: badlamer/hhvm
function ut_main()
{
    $fmt = ut_nfmt_create("en_US", NumberFormatter::CURRENCY);
    $currency = '';
    $pos = 0;
    $num = ut_nfmt_parse_currency($fmt, '123.45', $currency, $pos);
    if ($num === false) {
        return $fmt->getErrorMessage() . " (" . $fmt->getErrorCode() . ")\n";
    } else {
        return "Ooops, an error should have occurred.";
    }
}
コード例 #5
0
function ut_main()
{
    $locales = array('en_UK', 'en_US@California', 'fr_CA');
    $loc_types = array(Locale::ACTUAL_LOCALE => 'actual', Locale::VALID_LOCALE => 'valid');
    $res_str = '';
    foreach ($locales as $locale) {
        $fmt = ut_nfmt_create($locale, NumberFormatter::DECIMAL);
        $res_str .= "{$locale}: ";
        foreach ($loc_types as $loc_type => $loc_type_name) {
            $res_str .= sprintf(" %s=%s", $loc_type_name, dump(ut_nfmt_get_locale($fmt, $loc_type)));
        }
        $res_str .= "\n";
    }
    return $res_str;
}
コード例 #6
0
function ut_main()
{
    $res_str = '';
    $fmt = ut_nfmt_create("en_US", NumberFormatter::CURRENCY);
    $pos = 0;
    $currency = '';
    $num = ut_nfmt_parse_currency($fmt, '$9,988,776.65', $currency, $pos);
    $res_str .= "{$num} {$currency}\n";
    $fmt = ut_nfmt_create("en_US", NumberFormatter::CURRENCY);
    $pos = 1;
    $currency = '';
    $num = ut_nfmt_parse_currency($fmt, ' $123.45', $currency, $pos);
    $res_str .= "{$num} {$currency}\n";
    return $res_str;
}
コード例 #7
0
ファイル: formatter_parse.php プロジェクト: badlamer/hhvm
function ut_main()
{
    $res_str = '';
    // Test parsing float number.
    $fmt = ut_nfmt_create("en_US", NumberFormatter::DECIMAL);
    $res_str .= ut_nfmt_parse($fmt, "123E-3") . "\n";
    // Test parsing float number as integer.
    $fmt = ut_nfmt_create("en_US", NumberFormatter::DECIMAL);
    $res_str .= ut_nfmt_parse($fmt, "1.23", NumberFormatter::TYPE_INT32) . "\n";
    // Test specifying non-zero parsing start position.
    $fmt = ut_nfmt_create("en_US", NumberFormatter::DECIMAL);
    $pos = 2;
    $res_str .= ut_nfmt_parse($fmt, "0.123 here", NumberFormatter::TYPE_DOUBLE, $pos) . "\n";
    $res_str .= "{$pos}\n";
    return $res_str;
}
コード例 #8
0
function ut_main()
{
    // Array with data for testing
    $long_str = str_repeat('blah', 100);
    $attributes = array('POSITIVE_PREFIX' => array(NumberFormatter::POSITIVE_PREFIX, '_+_', 12345.1234), 'POSITIVE_SUFFIX' => array(NumberFormatter::POSITIVE_SUFFIX, '_+_', 12345.1234), 'NEGATIVE_PREFIX' => array(NumberFormatter::NEGATIVE_PREFIX, '_-_', -12345.1234), 'NEGATIVE_SUFFIX' => array(NumberFormatter::NEGATIVE_SUFFIX, '_-_', -12345.1234), 'PADDING_CHARACTER' => array(NumberFormatter::PADDING_CHARACTER, '^', 12345.1234), 'POSITIVE_PREFIX-2' => array(NumberFormatter::POSITIVE_PREFIX, $long_str, 12345.1234));
    $res_str = '';
    $fmt = ut_nfmt_create("en_US", NumberFormatter::DECIMAL);
    foreach ($attributes as $attr_name => $data) {
        list($attr, $new_val, $test_number) = $data;
        $res_str .= "\nAttribute {$attr_name}\n";
        if ($attr == NumberFormatter::PADDING_CHARACTER) {
            ut_nfmt_set_attribute($fmt, NumberFormatter::FORMAT_WIDTH, 21);
        }
        // Get default attribute's value
        $def_val = ut_nfmt_get_text_attribute($fmt, $attr);
        if ($def_val === false) {
            $res_str .= "get_text_attribute() error: " . ut_nfmt_get_error_message($fmt) . "\n";
        }
        $res_str .= "Default value: [{$def_val}]\n";
        $res_str .= "Formatting number with default value: " . ut_nfmt_format($fmt, $test_number) . "\n";
        // Set new attribute's value and see if it works out.
        $res_val = ut_nfmt_set_text_attribute($fmt, $attr, $new_val);
        if (!$res_val) {
            $res_str .= "set_text_attribute() error: " . ut_nfmt_get_error_message($fmt) . "\n";
        }
        // Get attribute value back.
        $new_val_check = ut_nfmt_get_text_attribute($fmt, $attr);
        $res_str .= "New value: [{$new_val_check}]\n";
        $res_str .= "Formatting number with new value: " . ut_nfmt_format($fmt, $test_number) . "\n";
        // Check if the new value has been set.
        if ($new_val !== $new_val_check) {
            $res_str .= "ERROR: New {$attr_name} symbol value has not been set correctly.\n";
        }
        // Restore attribute's value to default
        ut_nfmt_set_text_attribute($fmt, $attr, $def_val);
        if ($attr == NumberFormatter::PADDING_CHARACTER) {
            ut_nfmt_set_attribute($fmt, NumberFormatter::FORMAT_WIDTH, 0);
        }
    }
    //
    $fmt = ut_nfmt_create("uk_UA", NumberFormatter::CURRENCY);
    $res_str .= sprintf("\nCurrency ISO-code for locale 'uk_UA' is: %s\n", ut_nfmt_get_text_attribute($fmt, NumberFormatter::CURRENCY_CODE));
    return $res_str;
}
コード例 #9
0
ファイル: formatter_format.php プロジェクト: badlamer/hhvm
function ut_main()
{
    $styles = array(NumberFormatter::PATTERN_DECIMAL => '##.#####################', NumberFormatter::DECIMAL => '', NumberFormatter::CURRENCY => '', NumberFormatter::PERCENT => '', NumberFormatter::SCIENTIFIC => '', NumberFormatter::SPELLOUT => '@@@@@@@', NumberFormatter::ORDINAL => '', NumberFormatter::DURATION => '', NumberFormatter::PATTERN_RULEBASED => '#####.###', 1234999);
    $integer = array(NumberFormatter::ORDINAL => '', NumberFormatter::DURATION => '');
    $locales = array('en_US', 'ru_UA', 'de', 'fr', 'en_UK');
    $str_res = '';
    $number = 1234567.8912345679;
    foreach ($locales as $locale) {
        $str_res .= "\nLocale is: {$locale}\n";
        foreach ($styles as $style => $pattern) {
            $fmt = ut_nfmt_create($locale, $style, $pattern);
            if (!$fmt) {
                $str_res .= "Bad formatter!\n";
                continue;
            }
            $str_res .= dump(isset($integer[$style]) ? ut_nfmt_format($fmt, $number, NumberFormatter::TYPE_INT32) : ut_nfmt_format($fmt, $number)) . "\n";
        }
    }
    return $str_res;
}
コード例 #10
0
function ut_main()
{
    $longstr = str_repeat("blah", 10);
    $symbols = array('DECIMAL_SEPARATOR_SYMBOL' => array(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, '_._', 12345.123456, NumberFormatter::DECIMAL), 'GROUPING_SEPARATOR_SYMBOL' => array(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '_,_', 12345.123456, NumberFormatter::DECIMAL), 'PATTERN_SEPARATOR_SYMBOL' => array(NumberFormatter::PATTERN_SEPARATOR_SYMBOL, '_;_', 12345.123456, NumberFormatter::DECIMAL), 'PERCENT_SYMBOL' => array(NumberFormatter::PERCENT_SYMBOL, '_%_', 12345.123456, NumberFormatter::PERCENT), 'ZERO_DIGIT_SYMBOL' => array(NumberFormatter::ZERO_DIGIT_SYMBOL, '_ZD_', 12345.123456, NumberFormatter::DECIMAL), 'DIGIT_SYMBOL' => array(NumberFormatter::DIGIT_SYMBOL, '_DS_', 12345.123456, NumberFormatter::DECIMAL), 'MINUS_SIGN_SYMBOL' => array(NumberFormatter::MINUS_SIGN_SYMBOL, '_-_', -12345.123456, NumberFormatter::DECIMAL), 'PLUS_SIGN_SYMBOL' => array(NumberFormatter::PLUS_SIGN_SYMBOL, '_+_', 12345.123456, NumberFormatter::SCIENTIFIC), 'CURRENCY_SYMBOL' => array(NumberFormatter::CURRENCY_SYMBOL, '_$_', 12345.123456, NumberFormatter::CURRENCY), 'INTL_CURRENCY_SYMBOL' => array(NumberFormatter::INTL_CURRENCY_SYMBOL, '_$_', 12345.123456, NumberFormatter::CURRENCY), 'MONETARY_SEPARATOR_SYMBOL' => array(NumberFormatter::MONETARY_SEPARATOR_SYMBOL, '_MS_', 12345.123456, NumberFormatter::CURRENCY), 'EXPONENTIAL_SYMBOL' => array(NumberFormatter::EXPONENTIAL_SYMBOL, '_E_', 12345.123456, NumberFormatter::SCIENTIFIC), 'PERMILL_SYMBOL' => array(NumberFormatter::PERMILL_SYMBOL, '_PS_', 12345.123456, NumberFormatter::DECIMAL), 'PAD_ESCAPE_SYMBOL' => array(NumberFormatter::PAD_ESCAPE_SYMBOL, '_PE_', 12345.123456, NumberFormatter::DECIMAL), 'INFINITY_SYMBOL' => array(NumberFormatter::INFINITY_SYMBOL, '_IS_', 12345.123456, NumberFormatter::DECIMAL), 'NAN_SYMBOL' => array(NumberFormatter::NAN_SYMBOL, '_N_', 12345.123456, NumberFormatter::DECIMAL), 'SIGNIFICANT_DIGIT_SYMBOL' => array(NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL, '_SD_', 12345.123456, NumberFormatter::DECIMAL), 'MONETARY_GROUPING_SEPARATOR_SYMBOL' => array(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, '_MG_', 12345.123456, NumberFormatter::CURRENCY), 'MONETARY_GROUPING_SEPARATOR_SYMBOL-2' => array(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, " ", 12345.123456, NumberFormatter::CURRENCY), 'MONETARY_GROUPING_SEPARATOR_SYMBOL-3' => array(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $longstr, 12345.123456, NumberFormatter::CURRENCY));
    $res_str = '';
    foreach ($symbols as $symb_name => $data) {
        list($symb, $new_val, $number, $attr) = $data;
        $fmt = ut_nfmt_create('en_US', $attr);
        $res_str .= "\nSymbol '{$symb_name}'\n";
        // Get original symbol value.
        $orig_val = ut_nfmt_get_symbol($fmt, $symb);
        $res_str .= "Default symbol: [{$orig_val}]\n";
        // Set a new symbol value.
        $res_val = ut_nfmt_set_symbol($fmt, $symb, $new_val);
        if (!$res_val) {
            $res_str .= "set_symbol() error: " . ut_nfmt_get_error_message($fmt) . "\n";
        }
        // Get the symbol value back.
        $new_val_check = ut_nfmt_get_symbol($fmt, $symb);
        if (!$new_val_check) {
            $res_str .= "get_symbol() error: " . ut_nfmt_get_error_message($fmt) . "\n";
        }
        $res_str .= "New symbol: [{$new_val_check}]\n";
        // Check if the new value has been set.
        if ($new_val_check !== $new_val) {
            $res_str .= "ERROR: New {$symb_name} symbol value has not been set correctly.\n";
        }
        // Format the number using the new value.
        $s = ut_nfmt_format($fmt, $number);
        $res_str .= "A number formatted with the new symbol: {$s}\n";
        // Restore attribute's symbol.
        ut_nfmt_set_symbol($fmt, $symb, $orig_val);
    }
    $badvals = array(2147483648, -2147483648, -1, 4294901761);
    foreach ($badvals as $badval) {
        if (ut_nfmt_get_symbol($fmt, 2147483648)) {
            $res_str .= "Bad value {$badval} should return false!\n";
        }
    }
    return $res_str;
}
コード例 #11
0
function ut_main()
{
    // attr_name => array( attr, value )
    $attributes = array('PARSE_INT_ONLY' => array(NumberFormatter::PARSE_INT_ONLY, 1, 12345.123456), 'GROUPING_USED' => array(NumberFormatter::GROUPING_USED, 0, 12345.123456), 'DECIMAL_ALWAYS_SHOWN' => array(NumberFormatter::DECIMAL_ALWAYS_SHOWN, 1, 12345), 'MAX_INTEGER_DIGITS' => array(NumberFormatter::MAX_INTEGER_DIGITS, 2, 12345.123456), 'MIN_INTEGER_DIGITS' => array(NumberFormatter::MIN_INTEGER_DIGITS, 20, 12345.123456), 'INTEGER_DIGITS' => array(NumberFormatter::INTEGER_DIGITS, 7, 12345.123456), 'MAX_FRACTION_DIGITS' => array(NumberFormatter::MAX_FRACTION_DIGITS, 2, 12345.123456), 'MIN_FRACTION_DIGITS' => array(NumberFormatter::MIN_FRACTION_DIGITS, 20, 12345.123456), 'FRACTION_DIGITS' => array(NumberFormatter::FRACTION_DIGITS, 5, 12345.123456), 'MULTIPLIER' => array(NumberFormatter::MULTIPLIER, 2, 12345.123456), 'GROUPING_SIZE' => array(NumberFormatter::GROUPING_SIZE, 2, 12345.123456), 'ROUNDING_MODE' => array(NumberFormatter::ROUNDING_MODE, 1, 12345.123456), 'ROUNDING_INCREMENT' => array(NumberFormatter::ROUNDING_INCREMENT, (double) 2, 12345.123456), 'FORMAT_WIDTH' => array(NumberFormatter::FORMAT_WIDTH, 27, 12345.123456), 'PADDING_POSITION' => array(NumberFormatter::PADDING_POSITION, 2, 12345.123456), 'SECONDARY_GROUPING_SIZE' => array(NumberFormatter::SECONDARY_GROUPING_SIZE, 2, 12345.123456), 'SIGNIFICANT_DIGITS_USED' => array(NumberFormatter::SIGNIFICANT_DIGITS_USED, 1, 12345.123456), 'MIN_SIGNIFICANT_DIGITS' => array(NumberFormatter::MIN_SIGNIFICANT_DIGITS, 3, 1), 'MAX_SIGNIFICANT_DIGITS' => array(NumberFormatter::MAX_SIGNIFICANT_DIGITS, 4, 12345.123456));
    $res_str = '';
    $fmt = ut_nfmt_create("en_US", NumberFormatter::DECIMAL);
    foreach ($attributes as $attr_name => $args) {
        list($attr, $new_val, $number) = $args;
        $res_str .= "\nAttribute {$attr_name}\n";
        // Get original value of the attribute.
        $orig_val = ut_nfmt_get_attribute($fmt, $attr);
        // Format the number using the original attribute value.
        $rc = ut_nfmt_format($fmt, $number);
        $ps = ut_nfmt_parse($fmt, $rc);
        $res_str .= sprintf("Old attribute value: %s ;  Format result: %s ; Parse result: %s\n", dump($orig_val), dump($rc), dump($ps));
        // Set new attribute value.
        $rc = ut_nfmt_set_attribute($fmt, $attr, $new_val);
        if ($rc) {
            $res_str .= "Setting attribute: ok\n";
        } else {
            $res_str .= sprintf("Setting attribute failed: %s\n", ut_nfmt_get_error_message($fmt));
        }
        // Format the number using the new value.
        $rc = ut_nfmt_format($fmt, $number);
        // Get current value of the attribute and check if it equals $new_val.
        $attr_val_check = ut_nfmt_get_attribute($fmt, $attr);
        if ($attr_val_check !== $new_val) {
            $res_str .= "ERROR: New {$attr_name} attribute value has not been set correctly.\n";
        }
        $ps = ut_nfmt_parse($fmt, $rc);
        $res_str .= sprintf("New attribute value: %s ;  Format result: %s ; Parse result: %s\n", dump($new_val), dump($rc), dump($ps));
        // Restore original attribute of the  value
        if ($attr != NumberFormatter::INTEGER_DIGITS && $attr != NumberFormatter::FRACTION_DIGITS && $attr != NumberFormatter::FORMAT_WIDTH && $attr != NumberFormatter::SIGNIFICANT_DIGITS_USED) {
            ut_nfmt_set_attribute($fmt, $attr, $orig_val);
        }
    }
    return $res_str;
}