Float to_s behavior

Does wxruby change the behavior of to_s for float ?

If I run this little program

a = [1.23,4.56,7.89]
puts a.join(’ ')

I get the following output
1.23 4.56 7.89

As your would expect, but if I add it to a program that uses wxruby the
output is
1,23 4,56 7,89

According to danish grammar(I am from Denmark) this is the correct way
to write numbers but this charge mess with all data files I write.

How can I change it back to the original form ?

Hi

Svend Haugaard Sørensen wrote:

As your would expect, but if I add it to a program that uses wxruby the
output is
1,23 4,56 7,89

According to danish grammar(I am from Denmark) this is the correct way
to write numbers but this charge mess with all data files I write.

Interesting. wxRuby itself doesn’t do anything to alter the behaviour of
Float#to_s. The wxRuby library doesn’t change or extend any of Ruby’s
standard classes; we consider that bad behaviour for a library.

What’s happening is that wxWidgets somewhere is calling the C setlocale
function - I can’t quite pin it down in the Wx source at the moment.
This affects the way that the C sprintf (and also strftime) functions
work. Ruby calls these under the hood to implement methods like
Float#to_s and DateTime#strftime.

I see something similar to you. In my locale (en_GB) our standard way of
writing dates is DD/MM/YY (today is 24/08/2008), whereas in the US,
people write months MM/DD/YY (today would be 08/24/2008). When I change
wxRuby’s locale to be en_GB, it also affects the way that Ruby methods
like Time.now.strftime(’%x’) work.

How can I change it back to the original form ?

The easiest way is probably to explicitly switch to en_US locale in the
startup of your wxRuby application:

Wx::Locale.set_locale(‘en_US’)

See the documentation
http://wxruby.rubyforge.org/doc/locale.html

Examples of using Wx::Locale can be found in one of the tests:
http://wxruby.rubyforge.org/svn/trunk/wxruby2/tests/test_intl.rb

cheers
alex

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs