Issue Details (XML | Word | Printable)

Key: MDLSITE-548
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Eloy Lafuente (stronk7)
Reporter: Eloy Lafuente (stronk7)
Votes: 0
Watchers: 2
Operations

Add/Edit UI Mockup to this issue
If you were logged in you would be able to see more operations.
Moodle.org Sites

Format big numbers in stats with format_float(), for better readability

Created: 04/Nov/08 04:06 AM   Updated: 05/Nov/08 08:37 AM
Return to search
Component/s: moodle.org

Participants: Eloy Lafuente (stronk7), Martin Dougiamas and Petr Skoda
Security Level: None
QA Assignee: Helen Foster
Resolved date: 05/Nov/08


 Description  « Hide
Current numbers in http://moodle.org/stats are becoming so big that it's difficult to read them properly. Since Moodle 1.9 we have the cool format_float() function that is able to localise those numbers with points, commas and so on. Apply it to all the page numbers.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Eloy Lafuente (stronk7) added a comment - 04/Nov/08 04:07 AM
Getting this... I'll work in a http://moodle.org/stats/index2.php page.

Eloy Lafuente (stronk7) added a comment - 04/Nov/08 04:15 AM
Closing, as the new format_float() function only allows to localise decimal separator and not thousands separator (that can be comma, point or white space..).

http://moodle.org/stats/index2.php deleted

Ciao


Martin Dougiamas added a comment - 04/Nov/08 02:41 PM
How wierd, I never saw this bug earlier but actually fixed this page earlier today using number_format() (as well as lots of other formatting issues).

Is it OK if we use commas all the time? :-P


Martin Dougiamas added a comment - 04/Nov/08 03:05 PM
I put in a little function that SHOULD work but it's not for some reason ...

moodle_setlocale();

function localisenumber($number) {
$string = (string)number_format($number);
$locale = localeconv();
$sep = $locale['thousands_sep'];
if ($sep == ',' || $sep == '') { return $string; } else { return $string = str_replace(',', $sep, $string); }
}


Eloy Lafuente (stronk7) added a comment - 04/Nov/08 06:02 PM
Yes, localeconv() isn't working fine here (it only returns dec_sep properly defined).

In any case, as we have:

$string['decsep'] = '.'; // decimal point separator, for some languages it is ','

in en_utf8/langconfig.php

what about to add:

$string['thousandsep'] = '.'; // thousands point separator, for some languages it is '.'

And then, simply, we use it in private localisenumber() function above?

Ciao


Martin Dougiamas added a comment - 04/Nov/08 09:22 PM
Gosh that sounds very sensible.

Eloy Lafuente (stronk7) added a comment - 04/Nov/08 09:54 PM
Wow, just discovered that "sensible" in English mean "appropriate" (more or less). In Spanish, literally, it means "dangerous" and I was thinking why were you considering dangerous to add one lang string. LOL.

Oki, I'll add it...ciao Reopening this.


Eloy Lafuente (stronk7) added a comment - 05/Nov/08 02:55 AM
Done. Code:

function localisenumber($number, $numdec = 2) {

$decimalsep = get_string('decsep');
$thousandssep = get_string('thousandssep');

if (!is_numeric($number)) { return $number; }

if ((float)$number == (int)$number) { /// Integer number, no decimals return (string)number_format($number, 0, $decimalsep, $thousandssep); } else { /// Float number, apply decimals return (string)number_format($number, $numdec, $decimalsep, $thousandssep); }
}

Examples:

http://moodle.org/stats/?lang=es
http://moodle.org/stats/?lang=en


Petr Skoda added a comment - 05/Nov/08 04:36 AM
I hope you understand this is a major potential problem - mixing , . is going to be a real headache one day. Just printing "correct" formatting is no enough, it should be imho consistent with our parsing of floats when accepting user input. Mixed , . are going to break those automatic conversion hacks for oracle and we will never be able to guess decimal separator!

Also direct use of number_format() is not recommended anymore, we do have format_float() and unformat_float().


Petr Skoda added a comment - 05/Nov/08 04:38 AM
I understand /stats is not a core moodle stuff, I just want to make sure we agree on some separators handling for core code

Eloy Lafuente (stronk7) added a comment - 05/Nov/08 08:34 AM - edited
Yup 100% Petr.

IMO unformating/editing shouldn't accept thousand separators at all. But (non-editable) output... that's another story we could talk more.

Anyway, this has been exclusively for stats output, using its own function and blah, blah.

PS: Oracle? NLS_NUMERIC_CHARACTERS='.,' handles that completely (specifying both the dec and thousands separator). And it's in core since ages. NP (special) there AFAIK.