With a browser, how do I know which decimal separator does the operating system use?

后端 未结 13 2093
不思量自难忘°
不思量自难忘° 2020-11-27 17:05

I\'m developing a web application.

I need to display some decimal data correctly so that it can be copied and pasted into a certain GUI application that

相关标签:
13条回答
  • 2020-11-27 17:13

    I think you have to rely on JavaScript to give you the locale settings.
    But apparently JS doesn't have direct access to this information.
    I see Dojo Toolkit relies on an external database to find the locale information, although it might not take in account setting changes, for example.
    Another workaround I see is to have a small silent Java applet that query this information from the system, and JavaScript to get it out of Java.
    I can give more information if you don't know how to do it (if you want to go this convoluted route, of course).

    [EDIT] So I updated my knowledge of localization support in Java...
    Unlike what I thought originally, you won't have directly the decimal separator or thousand separator characters directly, like you would do with line separator or path separator: instead Java offers APIs to format the numbers or dates you provide.
    Somehow, it makes sense: in Europe you often put the currency symbol after the number, some countries (India?) have a more complex rule to separate digits, etc.

    Another thing: Java correctly finds the current locale from the system, but doesn't take information from there (perhaps for above reasons). Instead it uses its own set of rules. So if you have a Spanish locale where you replaced decimal separator with an exclamation sign, Java won't use it (but perhaps neither your application, anyway...).

    So I am writing an applet exposing a service (functions) to JavaScript, allowing to format numbers to the current locale. You can use it as such, using JavaScript to format numbers on the browser. Or you can just feed it with some sample number and extract the symbols from there, using them locally or feeding them back to the server.

    I finish and test my applet and post it there soon.

    0 讨论(0)
  • 2020-11-27 17:14

    Even if you knew what locale this "GUI Application" is running under, you still have to figure out how it is getting the current locale, and how it is determining the decimal separator.

    i don't know how it is done on a Mac, but on Windows applications are supposed to interrogte the user's preferences set via the Control Panel. It's quite possible this mystery applicaiton is ignoring those settings, and using their own internal setup instead.

    Or perhaps they're taking the current locale, and inferring the rest, rather than being told.

    Even then, in english, numbers are given in groups of 3 digits, with a comma separating the groups. i.e.:

    5,197,359,078
    

    Unless the number was an integer that contains a phone number:

    519-735-9078
    

    Unless of course the number was an integer that contains an account number:

    5197359078
    

    In which case, you're back to hard-coded overridden logic.

    Edit: Removed currency example, since currency has its own formatting rules.

    0 讨论(0)
  • 2020-11-27 17:15

    Another possible solution: You could use something like GeoIP (example in PHP) to determine the user's location and decide based on these information.

    0 讨论(0)
  • 2020-11-27 17:16

    I can guess the decimal separator from Accept-Language and the guess will be correct in 95% cases, but sometimes it fails.

    This is IMO the best course of action. In order to handle the failures, add a link to set it manually next to the display area.

    0 讨论(0)
  • 2020-11-27 17:17

    Here is a simple JavaScript function that will return this information. Tested in Firefox, IE6, and IE7. I had to close and restart my browser in between every change to the setting under Control Panel / Regional and Language Options / Regional Options / Customize. However, it picked up not only the comma and period, but also oddball custom things, like the letter "a".

    function whatDecimalSeparator() {
        var n = 1.1;
        n = n.toLocaleString().substring(1, 2);
        return n;
    }
    

    function whatDecimalSeparator() {
        var n = 1.1;
        n = n.toLocaleString().substring(1, 2);
        return n;
    }
    
    console.log('You use "' + whatDecimalSeparator() + '" as Decimal seprator');

    Does this help?

    0 讨论(0)
  • 2020-11-27 17:19

    Ask the user, do not guess. Have a setting for it in your web application.

    Edited to add:

    I think it is ok to guess the default setting that works ok, say, 95% of the time. What I meant was that the user should still be able to override whatever guesses the software made. I've been frustrated too many times already when a software tries to be too smart and does not allow to be corrected.

    0 讨论(0)
提交回复
热议问题