| 1 | <?php |
| 2 | /** |
| 3 | * wordpress-develop. |
| 4 | * User: Paul |
| 5 | * Date: 2016-02-01 |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | |
| 14 | /** |
| 15 | * Convert integer number to format based on the locale. |
| 16 | * |
| 17 | * @since 2.3.0 |
| 18 | * |
| 19 | * @global WP_Locale $wp_locale |
| 20 | * |
| 21 | * @param int $number The number to convert based on locale. |
| 22 | * @param int $decimals Optional. Precision of the number of decimal places. Default 0. |
| 23 | * @return string Converted number in string format. |
| 24 | */ |
| 25 | function XXnumber_format_i18n( $number, $decimals = 0 ) { |
| 26 | global $wp_locale; |
| 27 | |
| 28 | if ( isset( $wp_locale ) ) { |
| 29 | $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); |
| 30 | } else { |
| 31 | $formatted = number_format( $number, absint( $decimals ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Filter the number formatted based on the locale. |
| 36 | * |
| 37 | * @since 2.8.0 |
| 38 | * |
| 39 | * @param string $formatted Converted number in string format. |
| 40 | */ |
| 41 | return apply_filters( 'number_format_i18n', $formatted ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | class Tests_Functions_number_format_i18n extends WP_UnitTestCase { |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | */ |
| 51 | function test_number_format_i18n_number_is_not_number() { |
| 52 | |
| 53 | $this->assertEquals( '0', number_format_i18n( 'seven' ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * |
| 58 | */ |
| 59 | function test_number_format_i18n_decimal_is_not_number() { |
| 60 | |
| 61 | $this->assertEquals( number_format_i18n( 10, 'seven' ) , '10' ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * |
| 66 | */ |
| 67 | function test_number_format_i18n_large() { |
| 68 | global $wp_locale; |
| 69 | $wp_locale->number_format['decimal_point'] = '@'; |
| 70 | $wp_locale->number_format['thousands_sep'] = '^'; |
| 71 | |
| 72 | $this->assertEquals( '1^000@00', number_format_i18n( 1000.00, 2 ) ); |
| 73 | $this->assertEquals( '1^234^567^890@00000', number_format_i18n( 1234567890.00, 5 ) ); |
| 74 | // clear $wp_locale |
| 75 | $wp_locale = null; |
| 76 | } |
| 77 | /** |
| 78 | * |
| 79 | */ |
| 80 | function test_number_format_i18n_no_global_and_us() { |
| 81 | |
| 82 | $this->assertEquals( '1,000.00', number_format_i18n( 1000.00, 2 ) ); |
| 83 | $this->assertEquals( '1,234,567,890.00000', number_format_i18n( 1234567890.00, 5 ) ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | */ |
| 89 | function test_number_format_i18n_brazil() { |
| 90 | global $wp_locale; |
| 91 | $wp_locale->number_format['decimal_point'] = ','; |
| 92 | $wp_locale->number_format['thousands_sep'] = '.'; |
| 93 | |
| 94 | $this->assertEquals( '1.000,00', number_format_i18n( 1000.00, 2 ) ); |
| 95 | $this->assertEquals( '1.234.567.890,00000', number_format_i18n( 1234567890.00, 5 ) ); |
| 96 | // clear $wp_locale |
| 97 | $wp_locale = null; |
| 98 | } |
| 99 | } |
| 100 | No newline at end of file |