Git Product home page Git Product logo

Comments (4)

BeksOmega avatar BeksOmega commented on May 5, 2024 1

I've looked into this a bit before, and it's actually quite a complicated problem to solve smoothly.

Some users want the decimal seperator to match their system/language settings. Others want it to always be a period regardless of their settings. Some people want it to work differently when they're copy-pasting vs when they're typing directly. Etc etc.

For an interesting discussion of this issue I recommend microsoft/calculator#153

from blockly-samples.

alschmiedt avatar alschmiedt commented on May 5, 2024

We will definitely look into this as a feature request. In the meantime, if you need this before we get to it you can look at creating a custom field that extends doClassValidation_. If you have any questions feel free to ask on our forum!

from blockly-samples.

Adam-Pasco avatar Adam-Pasco commented on May 5, 2024

Thanks for the quick responses.
BeksOmega, thanks for the link! That IS an interesting discussion.

from blockly-samples.

maribethb avatar maribethb commented on May 5, 2024

This is something we'd support, but is not currently in our planning timeline. This would be arguably be breaking change in behavior so something we'd want to wait on until we were going to do a major release. Therefore instead of changing the default number field, this should be a new field plugin. Anyone who wants this behavior could unregister the default number field and register the plugin one in its place.

Here's an initial plan, which we could certainly discuss further at the time this is picked up.

For output (i.e. the field's text), we can convert the number to a string using toLocaleString and passing in the locale. Currently, we don't store the locale anywhere. Options:

  • We could change the Blockly.setLocale method to store the name of the locale that is passed in, so we can use it later.
    • This only captures language and not country. So, for the locale fr the difference between France and Quebec would not be captured.
    • Also assumes the blockly locales match the standard ones, an assumption that needs validating.
  • We could use navigator.language(s) to get the locale of the browser.
    • May not match the language Blockly is displayed in, which may be confusing.

For input, it's more complicated.

Today's behavior, inside doClassValidation_: commas are assumed to be thousands separators and removed. The string value, after this and other unrelated transformations, is converted to a number using the Number function. Thus, a single period is assumed to be a decimal place, and multiple periods are an error.

Desired behavior: Inspired by the microsoft/calculator discussion above, we'll let people enter commas and periods, regardless of their locale.

  • If both periods and commas appear, we'll assume that the last one in the string is for decimals, and any others are the thousands separators. If you enter two of the character used for decimals, that's a validation error (same as today).
  • If only one type appears, but multiple times, then assume it is a thousands separator and ignore it.
  • Ambiguous case: exactly one separator appears. Options:
    • Just assume it's a decimal because nobody types thousands separators.
    • Check the user's locale and assume they are being using that.
      • but if they are in a locale with commas as the decimal separator, this is confusing to people with experience with text-based programming who are accustomed to using periods anyway. Thus, this does not seem like a good option.
  • Internally, we want to store the value of the block as a number, not a string. So if the decimal character is a comma, we'd first need to strip out any existing periods, and then convert the comma to a period before casting the string to a number.

Additional context from Neil:

  • Blockly has historically ignored this because it is assumed that users are learning to code and will graduate to text-based programming, where they will need to learn to use the period for the decimal. But for other use cases of Blockly like configuration this does not apply.
  • Important to keep in mind that language only does not tell the full story. Quebec uses American-style numbers and dates which are different than in France. But the Blockly language code for both is 'fr'.

from blockly-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.