Float <-> String - Locale aware?
-
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.
-
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.
-
... 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.
-
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?
-
@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.
-
@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.
-
@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
-
-
@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. -
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...
-
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")
-
@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.
-
@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
-
Why won't
"12,2".to_l.to_f
work? -
@thomthom said:
Found a nice method:
...[snip]...
Example, Norwegian locale and 1 digit precision:
Sketchup.format_degrees(500.555)
Returns the string:"~ 500,6"
What happens when:
Sketchup.format_degrees(500,555)
Wouldn't you be specifying decimals in euro-format?
When I try it there is a conflict; Ruby sees the comma as an argument delimiter, and pukes up an argument (2 for 1) exception. -
@cjthompson said:
Why won't
"12,2".to_l.to_f
work?I can't test that; it raises an:
<ArgumentError: (eval):0:in
to_l': Cannot convert "12,2" to Length>` because I think of my US-en locale settings.
TT will need to test it, with his euro settings.Of course,
"12.2".to_l.to_f
works just fine for me (and anyone in the US.) -
@dan rathbun said:
@cjthompson said:
Why won't
"12,2".to_l.to_f
work?Of course,
"12.2".to_l.to_f
works just fine for me (and anyone in the US.)I take that back.
I set Format=Decimal, Units=Millimeters, Precision=0.0000mm, and the value returned (in Inches,) is:
"12.2".to_l.to_f >> 0.480314960629921
which is a precision to 15 decimal places. [FYI: 12.7mm is considered 0.5 inches. You often see 50 caliber weapons expressed as 12.7mm] -
@dan rathbun said:
(1) what does length then appear as in the console ?
length = "5,43".to_l
0.213779527559055Because I use metric mm template it also converts the float into inches.
@dan rathbun said:
(2) what does it appear as in an inputbox control ?
Because.to_s
is called upon the Length object.@dan rathbun said:
(3) what does it appear as in a webdialog ?
You'd have to convert it to a string to pass it to a webdialog, so the result would be the same as above.
-
@dan rathbun said:
What happens when:
length = "5.43".to_l
length = "5.43".to_l Error: #<ArgumentError: (eval):894:in
to_l': Cannot convert "5.43" to Length>
(eval):894` -
@cjthompson said:
Why won't
"12,2".to_l.to_f
work?"12,2".to_l.to_f 0.480314960629921
It doesn't work because SU does unit conversion at the same time.
Advertisement