number_format_i18n function doesn't check if the number is float or not before doing the formating
|Reported by:||chombium||Owned by:||nbachiyski|
I've noticed that there are problems in representation of decimal numbers. The number representation is defined by the following three values in the wordpress.pot translation file: number_format_decimals, number_format_decimal_point and number_format_thousands_sep.
In the Macedonian language translation of wordpress.pot which I
maintain, I've set this values to:
number_format_decimals = 3
number_format_decimal_point = ,
number_format_thousands_sep = .
This means that the proper decimal number representation would be 1.234,567
I've noticed that all the numbers including integers are shown with this format, so for example, I get "23,000" posts on the dashboard instead of "23".
I can fix this behavior by setting:
number_format_decimals = 0
in the translation, but if a floating point number has to be formated it will be truncated to its integer part.
This way all the floating point numbers are not formated correctly, but as I was told by Nikolay Bachiyski, this is the safe way to solve this issue because this function is called only with integers.
I dig trough the code of wp 2.8.3 and I've noticed that the problematic function is number_format_i18n located in wp-includes/functions.php
This function is the only place where php number_format function is
called with the parameters from the translation file.
The function number_format_i18n has to be patched so that first it checks what kind of number should be formated (integer or float) and then to do the formating. This means it has to set the correct number of decimal places before calling the php number_format function.
This can be done by adding the line:
if (!is_float($number)) $decimals = 0;
after setting the number of decimals, the line:
$decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals );
The proposed patch simply checks if the number which has to be formated is not float and if that is the case it sets the number of decimals to 0 ($decimals = 0)
I don't know if the file: wp-content/plugins/akismet/akismet.php has to be patched as well because it redeclares the number_format_i18n function if it doesn't exist. I see that the function is called with one parameter in this file, which means decimals = 0.
The diff file of wp-includes/functions.php is in the attachment.
Change History (25)
- Keywords number formating i18n removed
- Milestone changed from Unassigned to 2.9
comment:12 follow-up: ↓ 13 nbachiyski — 4 years ago
- Keywords dev-feedback added; needs-testing early removed
- Status changed from new to accepted
comment:16 in reply to: ↑ 15 westi — 4 years ago
- Resolution fixed deleted
- Status changed from closed to reopened