Comma vs period as decimal seperator

110 views
Skip to first unread message

Uwe K

unread,
Feb 24, 2019, 5:36:08 PM2/24/19
to Blockly
Hello,

I'd like have the number field use the format 1,2 for some users and 1.2 for others, depending on language settings.
The source code looks like there is no toggle of config option for that.
Is overwriting classValidator the way to do this?

cheers,
Uwe

Neil Fraser

unread,
Feb 24, 2019, 8:35:07 PM2/24/19
to blo...@googlegroups.com
Yes, classValidator is exactly where this goes.  There's even a TODO at the right spot:

Historical side note: The original reason we didn't support localized numbers was because Blockly's initial use-case was to teach programming.  Rather than subject students to a format change later, we chose to start with the format universally used in programming languages.  However, with Blockly now being used extensively outside of its initially envisioned role as an intro to programming, it makes absolute sense to support localized numbers.


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Uwe K

unread,
Mar 24, 2019, 6:27:39 AM3/24/19
to Blockly
This is more tricky than I thought.

Accepting both comma and period as decimal seperators only works if they are no longer accepted as thousands seperators. Otherwise 1,000 and 1.000 could both mean 1 or 1000.

I tried to make it two different modes by adding a workspace option *useDecimalComma*. In comma-mode, the number field should display and accept as input numbers like 1,2 and reject 1.2. Now I had a problem with the field's text/value distinction. The field's value should be used for code generation and block serialization, while the field's text should be displayed to to user. So, in comma-mode, a text of "1,2" should correspond to a value of "1.2".

But the number field inherits from text field, where value and text are always the same. It sets the field value directly from the user's input. I tried to add a method *setValueFromUserInput*, where the number field can do comma replacement. That works, but turned into more changes than I'm comfortable with.

For now I'll go with the simple solution of accepting both comma and period input and displaying only periods.
Reply all
Reply to author
Forward
0 new messages