• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Float <-> String - Locale aware?

Scheduled Pinned Locked Moved Developers' Forum
35 Posts 5 Posters 9.0k Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    thomthom
    last edited by 16 May 2010, 11:27

    Is there a method to convert between strings and floats that are locale aware?

    Some languages, like Norwegian, uses comma as decimal separator. But the String class' .to_f appears to ignore this and requires a period. Likewise with Float.to_s which returns the string with a period.

    Is there any methods that do these conversions that are locale aware?

    Thomas Thomassen β€” SketchUp Monkey & Coding addict
    List of my plugins and link to the CookieWare fund

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 16 May 2010, 21:04

      @thomthom said:

      Is there a method to convert between strings and floats that are locale aware?

      NO. For example there is no $global that sets the decimal separator character like there is for the record separator.

      REASON: Assignment.

      a=1,23
      returns a as pointing to an Array object [1,23].

      a,b=1,23
      returns a as pointing to the Integer object 1; and b as pointing to the Integer object 23. ("the" because Integer objects are an ordinal set of immediate objects, and there are only 1 of each in the set.)

      The "thousands" separator for Numeric literals OR Numeric strings, is the _ (underscore) character in Ruby. (This was done because the comma was already taken for use in assignment and as the parameter/argument separator.)

      "1_000.23".to_f
      returns 1000.23

      "1.23".to_f
      returns 1.23

      "1_23".to_f
      returns 123.0

      "1,23".to_f
      returns 1.0

      For .to_f and .to_s methods, ONLY Numeric characters are considered from the beginning of the expression up to the first occurance of a Non-Numeric character.
      The last example shows that the comma is not considered in the set of Numeric characters,ie:
      0..9
      _ (underscore)
      . (decimal point/period)
      - (negative/minus sign)
      + (positive/plus sign)

      "=1.23".to_f
      OR
      "(1.23)".to_f
      both return 0.0
      The methods do not raise an exception, instead if the first character is a Non-Numeric, 0.0 is returned.

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 16 May 2010, 22:02

        @dan rathbun said:

        REASON: Assignment.

        Nah, not a reason. I'm not talking about writing a = 1,3 within the code. But user input/output.

        It's converting between strings and floats. Input from the user, via the VCB or an Inputbox - where the user would type "1,23". There should be a method to convert that to a float. Same thing for the other way around. One should be able to convert 1.23 to a string in the user's locale.

        I find no method that does this, and I see no easy way to get the locale settings without messing with Win32 API on Windows and who knows what on OSX.

        String.to_l and Length.to_s does take into consideration the model unit and the user locale.

        I use a Norwegian locale. In the console:
        '25,4'.to_l 1.0

        '25.4'.to_l Error: #<ArgumentError: (eval):894:into_l': Cannot convert "25.4" to Length>
        (eval):894`

        So why not for floats?

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 16 May 2010, 22:25

          @thomthom said:

          @dan rathbun said:

          REASON: Assignment.

          Nah, not a reason. I'm not talking about writing a = 1,3 within the code. But user input/output.

          Yes I understand, but the basic Ruby reason is what I said in my previous post.

          I had to break for dinner, and was going to post again discussing overridding the methods String.to_f and Float.to_s, which can be done.
          Something like this?

          
          class String
          
            alias_method( ;old_to_f, ;to_f )
          
            def self.to_f( arg )
              return self.old_to_f( arg.sub(/[,]/,'.') )
            end
          
          end #class
          
          

          BTW, I remember another thread on this subject regarding the VCB.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 16 May 2010, 22:46

            Converting comma to period for the strings before one call .to_f works. It is what I am doing at the moment. (But as a separate method. I never override any base classes in SU Ruby. I even avoid adding new methods.) But the problem is the other way around. When one wants to present a float formatted in the format of the user's locale.

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 16 May 2010, 22:47

              @dan rathbun said:

              Yes I understand, but the basic Ruby reason is what I said in my previous post.

              I don't see how multiple assignment has anything to do with parsing strings with commas?

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 16 May 2010, 22:57

                @thomthom said:

                @dan rathbun said:

                Yes I understand, but the basic Ruby reason is what I said in my previous post.

                I don't see how multiple assignment has anything to do with parsing strings with commas?

                It doesn't, I was speaking about how Ruby interprets Numeric literals to create Float objects. Basically the comma was already taken as an argument and assignment separator.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 16 May 2010, 22:59

                  Well yes, but using commas in Numeric literals is not the problem in question.
                  It's string interpretation and presentation.

                  Here's a terrible hacky way:

                  r = 1 / '1'.to_l; x = '1,23'.to_l * r; x.to_f

                  Returns 1.23 for the string '1,23' when the locale uses comma for decimal separation.

                  But I really would like to use a clean way of doing this.

                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    thomthom
                    last edited by 16 May 2010, 23:03

                    πŸ˜’ The spider bots are quick. As I try to google Ruby and locale ware Float <-> String conversion I get this thread at the top of the results. πŸ‘Š

                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      thomthom
                      last edited by 16 May 2010, 23:09

                      ... I must be going mad. I dunno why I produced that previous hack when a simple replacement of comma to period works.
                      I meant to be tackling the other way around...

                      Ok - enough ramblings. Time to get some rest.

                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        thomthom
                        last edited by 16 May 2010, 23:15

                        No rest for the wicked.

                        decimal_separator = 1.2.to_l.to_s.match(/\d(\D)\d/)[1]
                        This returns the decimal separator.

                        1.23.to_s.tr!('.', decimal_separator)
                        And this outputs a float in the user's locale.

                        Hacky hacky hacky... 🀒

                        Now: how does one do this properly?

                        Thomas Thomassen β€” SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          thomthom
                          last edited by 16 May 2010, 23:17

                          @dan rathbun said:

                          We can see if the user is using metric, via:

                          Still doesn't say anything about the user's decimal separator though. 😞 Only their modelling units.
                          An user with English locale might use a metric template.

                          And one can set the decimal separator to anything in the system settings.

                          Thomas Thomassen β€” SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            Dan Rathbun
                            last edited by 16 May 2010, 23:33

                            @thomthom said:

                            Now: how does one do this properly?

                            The PROPER Ruby way is to either override a method, or add a new method to the base class. That's why I love Ruby. It's the Dynamic feature of the language.

                            Why is it that @Last/Google can override base classes (and do it very poorly/error prone, etc.) and I with 30+ years experience am not supposed to be allowed to?

                            Why does noone say anything when Rick Wilson overrides or adds to base classes or Sketchup classes?

                            The SKX project is/was a means of proposing/testing such new methods or base class overrides.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • D Offline
                              Dan Rathbun
                              last edited by 17 May 2010, 00:57

                              @thomthom said:

                              decimal_separator = 1.2.to_l.to_s.match(/\d(\D)\d/)[1]
                              This returns the decimal separator.

                              1.23.to_s.tr!('.', decimal_separator)
                              And this outputs a float in the user's locale.

                              No, it returns a String representation of a Float, using the user's locale system decimal separator.
                              But .tr/.tr! will change ALL occurances of '.', whereas sub/sub! change only the first occurance.

                              πŸ˜† picky, picky

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • T Offline
                                thomthom
                                last edited by 17 May 2010, 08:57

                                @dan rathbun said:

                                But .tr/.tr! will change ALL occurances of '.', whereas sub

                                Does it matter?

                                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                List of my plugins and link to the CookieWare fund

                                1 Reply Last reply Reply Quote 0
                                • T Offline
                                  thomthom
                                  last edited by 17 May 2010, 09:13

                                  @dan rathbun said:

                                  @thomthom said:

                                  Now: how does one do this properly?

                                  The PROPER Ruby way is to either override a method, or add a new method to the base class. That's why I love Ruby. It's the Dynamic feature of the language.

                                  Why is it that @Last/Google can override base classes (and do it very poorly/error prone, etc.) and I with 30+ years experience am not supposed to be allowed to?

                                  Why does noone say anything when Rick Wilson overrides or adds to base classes or Sketchup classes?

                                  The SKX project is/was a means of proposing/testing such new methods or base class overrides.

                                  Regardless of who does it, overriding base class method in the SU environment is a no-no. One can not know how it will affect other scripts - they could be relying on the original behaviour.
                                  I would love to override existing methods, but that only works if you have the sole control of the environment. And in SU Ruby, you don't.

                                  The reason I avoid adding new methods is that one never knows if someone else also adds a method of the same name that does something different. Same problem as before.
                                  Now, one could go about and prefix with your initial as the conventions are for root modules. But then again, say you create a a plugin that adds a method to a base class. It turns out to have a bug.
                                  You fix that.
                                  You then make another plugin with that fix.

                                  But the a user could have your new plugin and the old bugged version of your first and it loads last overriding the method. Now you're stuck with the bugged version in both your plugins - because you don't have control over the environment your plugin lives on.

                                  Now, this latter case is more of a problem for yourself, so if one want to do so then fine. But I avoid it.
                                  But overriding base methods are still a no no.

                                  I've seen Rick add to base classes. I've not seen him override base methods though.

                                  @dan rathbun said:

                                  Why is it that @Last/Google can override base classes (and do it very poorly/error prone, etc.) and I with 30+ years experience am not supposed to be allowed to?

                                  Because they deploy the platform. They are the source.
                                  You, me, everyone else got to try to live along side inside that environment without causing conflicts for each others.

                                  @dan rathbun said:

                                  The SKX project is/was a means of proposing/testing such new methods or base class overrides.

                                  Yes - we where even talking with people from Google Sketchup if they thought some of it could cause conflicts.
                                  Personally that project ended up being down-prioritised for other projects.

                                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    thomthom
                                    last edited by 17 May 2010, 10:21

                                    Found a nice method:
                                    http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#format_degrees

                                    It formats any numeral into a string using the user's locale with the model precision.

                                    Example, Norwegian locale and 1 digit precision:
                                    Sketchup.format_degrees(500.555)
                                    Returns the string: "~ 500,6" πŸ˜„

                                    Now, if only I could find a method for the other way around...

                                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                    List of my plugins and link to the CookieWare fund

                                    1 Reply Last reply Reply Quote 0
                                    • J Offline
                                      Jim
                                      last edited by 17 May 2010, 16:51

                                      It's OK to extend only specific instances with with some instance methods, and without modifying the base class.

                                      Something like...

                                      
                                      module Translatable
                                          def translate(lang)
                                              "Hi"
                                          end
                                      end
                                      
                                      str = "Hello"
                                      str.extend(Translatable)
                                      str.translate("en")
                                      
                                      
                                      

                                      Hi

                                      1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        Dan Rathbun
                                        last edited by 17 May 2010, 16:57

                                        @jim said:

                                        It's OK to extend only specific instances with with some instance methods, and without modifying the base class.

                                        Called creating a singleton method.

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • D Offline
                                          Dan Rathbun
                                          last edited by 17 May 2010, 17:08

                                          @thomthom said:

                                          Now, if only I could find a method for the other way around...

                                          I don't think I can test the other way as my locale setting uses period as decimal point.

                                          If your model units are metric, and your OS locale decimal separator is comma, what happens when you try to use the base class (SU extended) method String.to_l ?

                                          Ex:
                                          length = "5,43".to_l
                                          (1) what does length then appear as in the console ?
                                          (2) what does it appear as in an inputbox control ?
                                          (3) what does it appear as in a webdialog ?

                                          What happens when:
                                          length = "5.43".to_l

                                          I'm not here much anymore.

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 1 / 2
                                          1 / 2
                                          • First post
                                            1/35
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement