number field comma truncation

number field comma truncation

by Danny Wahl -
Number of replies: 6

If you input a number with comma notation in a number field the result is truncated before the comma.  E.g. "9,000" becomes "9".  Even explicitly adding a decimal gets truncated "9,000.00" becomes "9"

Looking at the php documentation all I can find is this:

PHP doesn't handle strings like "12,300.2" correctly by default.

with more information not showing a that exact usage case:

converting from strings

so, is this a PHP bug (in that it doesn't handle it by default) or a moodle bug in that we didn't handle that usage case, or a user bug for not understanding the nature of floating point operations.

the offending code seems to be a call to format_float in field/number/field.class.php and lib/moodlelib.php contains the function (although it's odd that a few qtypes define their own float)

Average of ratings: -
In reply to Danny Wahl

Re: number field comma truncation

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Be careful. It is not that simple.

The character used as the decimal place marker varies by language (it can be either ',' or '.'). Similarly the character used as the thousand separator can be either '.', ',' or ' '.

Therefore, something like 1,000 is ambiguous. In some contexts that will be interpreted as 1000. In others as 1.

Moodle language packs have to strings to tell you which characters to use, and there are functions format_float to output a PHP float formatted the way the user expects, and unformat_float to reverse that on input. That comes close to getting it right.

But, in reality, even that is not perfect, because of places like Québec, which is English-French bi-lingual. Those two languages have opposite conventions for how to write numbers, but one person living there will might input numbers either way. You can't win.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: number field comma truncation

by Danny Wahl -

You can't win.

Thanks, that's what I was actually hoping to hear- it'll save me more time of investigation.  I figured that localization was the cause. smile

however, there might still be a bug, as (if I under stand FP) 9,100 should be truncated to 9,1 or 9.1 depending on your flavor, but it's getting truncated to 9

In reply to Danny Wahl

Re: number field comma truncation

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There are two completely separate things here, which confusingly often look similar.

floating point numbers in PHP notation, like 1.0 or 1 or 3.0e8.

floating point numbers either input by the end user, or output to be seen by the end user, which in PHP terms are strings and depending on the locale are either

  • like '1.0' or '1' or '300,000,000'
  • like '1,0' or '1' or '300.000.000'

In Moodle, you should always use format_float and unformat float when you need to translate between the two.

When processing input, PARAM_FLOAT means a PHP-style float (that you might get passed directly behind the scenes from, say a slider widget). For user input you should  use PARA_RAW then pass it for unformat_float.

That is not perfect, but covers 99% of cases.

In reply to Tim Hunt

Re: number field comma truncation

by Danny Wahl -

Thanks Tim for your continued input- but if I understand correctly there should NEVER be a case, regardless of localization, where significant digits are truncated- which is happening- whether the fault of Moodle or PHP I don't know.

In reply to Danny Wahl

Re: number field comma truncation

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Do you know how computers store floating-point numbers? The way it works is nobody's fault, but wishing it was other than it is, is futile.

As William Lu said, if you care about the sequence of characters that make up the number, not the numerical value, then you have to treat the number as a string.
In reply to Danny Wahl

Re: number field comma truncation

by William Lu -
Picture of Particularly helpful Moodlers

I got similar issue before. 

My work around: don't use number filed, use text field instead and works fine for me.

Also, don't use comma thousands, use pure number like 1000 only.