简体   繁体   English

Ruby 1.8.6 BigDecimal.to_f在Solaris上始终返回'0,0'

[英]Ruby 1.8.6 BigDecimal.to_f always returns '0,0' on Solaris

I have come across a very weird error. 我遇到了一个非常奇怪的错误。 I'm on Solaris 10, using Ruby Enterprise Edition (ruby 1.8.6 (2008-08-08 patchlevel 286) [i386-solaris2.10]) with Rails 2.3.4. 我在Solaris 10上,使用带有Rails 2.3.4的Ruby Enterprise Edition(红宝石1.8.6(2008-08-08补丁程序286)[i386-solaris2.10])。 I have a very weird error. 我有一个很奇怪的错误。 In irb: 在irb中:

irb(main):001:0> require 'bigdecimal'
=> true
irb(main):002:0> b = BigDecimal.new('123')
=> #<BigDecimal:834d0e8,'0.123E3',4(8)>
irb(main):003:0> b.to_s
=> "0.123E3"
irb(main):004:0> b.to_i
=> 123
irb(main):005:0> b.to_f
=> 123.0
irb(main):006:0> 

Everything's fine! 一切安好! BUT when I fire up the Rails console and do the same thing, "to_f" always returns '0,0'. 但是,当我启动Rails控制台并执行相同的操作时,“ to_f”始终返回“ 0,0”。

>> b = BigDecimal.new('123')
=> #<BigDecimal:9e80e14,'0.123E3',4(8)>
>> b.to_s
=> "123.0"
>> b.to_i
=> 123
>> b.to_f
=> 0,0

This does not happen when I do the same thing on my Mac. 当我在Mac上执行相同操作时,不会发生这种情况。 Very weird! 很奇怪! Is that possibly a bug in Ruby Enterprise Edition? 这可能是Ruby Enterprise Edition中的错误吗? But if so, why doesn't it occur with irb (which also is the REE version, I double checked). 但是,如果是这样,为什么它不与irb一起发生(这也是REE版本,我再次检查了一下)。 Any ideas? 有任何想法吗?

  • Johannes 约翰尼斯

I'm no expert, but my guess is that this behavior is a side effect of your being in Germany. 我不是专家,但是我的猜测是这种行为是您在德国的副作用。

Apparently, to_f uses a nationalized version of the decimal separator. 显然,to_f使用十进制分隔符的国家化版本。 I suspect that if you change your computer's nationality settings to US, this will change. 我怀疑如果您将计算机的国籍设置更改为“美国”,那将会改变。

EDIT: 编辑:

Not that this helps your problem. 并不是说这可以解决您的问题。 But a little more background info might: 但是更多的背景信息可能会:

When you do 123.to_f in irb, you're actually executing 123.to_f.to_s (because irb needs to print out your result). 当你123.to_f在IRB,你实际上执行123.to_f.to_s (因为IRB需要打印出您的结果)。

You might be better served with explicit formatting using the format operator % : 使用格式运算符%显式格式化可能会更好。

"%5.1f" % 123 => 123.0

EDIT: 编辑:

After some digging, found some truly relevant and helpful information here: 经过一番挖掘,在这里找到了一些真正相关和有用的信息:

http://rubyforge.org/forum/forum.php?thread_id=32460&forum_id=723 http://rubyforge.org/forum/forum.php?thread_id=32460&forum_id=723

It's a problem that's known in the Ruby community, but will not be "fixed" as such. 这是Ruby社区中已知的问题,但不会这样“解决”。 Workarounds are provided in the thread. 线程中提供了解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM