Moodle Community Sites

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

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Component/s: moodle.org
  • Labels:
    None

Description

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.

Activity

Hide
Eloy Lafuente (stronk7) added a comment -

Getting this... I'll work in a http://moodle.org/stats/index2.php page.

Show
Eloy Lafuente (stronk7) added a comment - Getting this... I'll work in a http://moodle.org/stats/index2.php page.
Hide
Eloy Lafuente (stronk7) added a comment -

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

Show
Eloy Lafuente (stronk7) added a comment - 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
Hide
Martin Dougiamas added a comment -

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

Show
Martin Dougiamas added a comment - 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
Hide
Martin Dougiamas added a comment -

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); }
}

Show
Martin Dougiamas added a comment - 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); } }
Hide
Eloy Lafuente (stronk7) added a comment -

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

Show
Eloy Lafuente (stronk7) added a comment - 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
Hide
Martin Dougiamas added a comment -

Gosh that sounds very sensible.

Show
Martin Dougiamas added a comment - Gosh that sounds very sensible.
Hide
Eloy Lafuente (stronk7) added a comment -

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.

Show
Eloy Lafuente (stronk7) added a comment - 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.
Hide
Eloy Lafuente (stronk7) added a comment -

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

Show
Eloy Lafuente (stronk7) added a comment - 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
Hide
Petr Škoda (skodak) added a comment -

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().

Show
Petr Škoda (skodak) added a comment - 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().
Hide
Petr Škoda (skodak) added a comment -

I understand /stats is not a core moodle stuff, I just want to make sure we agree on some separators handling for core code

Show
Petr Škoda (skodak) added a comment - I understand /stats is not a core moodle stuff, I just want to make sure we agree on some separators handling for core code
Hide
Eloy Lafuente (stronk7) added a comment - - 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.

Show
Eloy Lafuente (stronk7) added a comment - - 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.

Dates

  • Created:
    Updated:
    Resolved: