On Jun 3, 2009, at 7:12 PM, Craig White wrote:
> On Wed, 2009-06-03 at 23:49 +0200, Marnen Laibow-Koser wrote:
>> Craig White wrote:
>> [...]
>>> ----
>>> well, the round(2) function will return 1.5 if that is the stored
>>> value
>>> in big decimal which was unacceptable
>>
>> Huh? I don't understand what you're saying.
>>
>>> but I didn't try
>>> @price.round(2).to_f but I wonder which is faster/slower.
>>>
>>> In reality, though, it tossed an error...
>>
>> Again, I don't understand. Did you try it or not?
>>
>>>
>>> undefined method `round' for #<Price:0xb6e1bf20>
>>>
>>> which I think is back to my original problem of having a big
>>> decimal,
>>> that must be converted to a float before it can be rounded.
>>
>> No. BigDecimal#round should work as I just explained -- at least it
>> does on my system. Are you sure @price is actually holding a
>> BigDecimal?
> ----
> console session...
>
> _>> @price = Price.find(:first, :conditions => ["stockid = ?",
> "F34/CW/ES"])
> => #<Price stockid: "F34/CW/ES", typeabbrev: "RE", currabrev: "USD",
> debtorno: " ", price: #<BigDecimal:b7dc83ec,'0.15E1',8(12)>,
> branchcode:
> " ">
> _>> @price.price
> => #<BigDecimal:b7dacaac,'0.15E1',8(12)>
> _>> @price.price.round(2)
> => #<BigDecimal:b7d9a4b0,'0.15E1',8(16)>
> _>> @price.price.round(2).to_f
> => 1.5
> _>> @price.price.round(2).to_fl(2)
> NoMethodError: undefined method `to_fl' for
> #<BigDecimal:b7ee9f64,'0.15E1',8(16)>
> from (irb):8
> _>> @price.price.round(2).to_f.to_fl(2)
> => "1.50"
> _>> @price.price.to_f.to_fl(2)
> => "1.50"
> _>> @price.price.to_f.round(2)
> => 1.5
>
> Craig
I think you two are talking past each other a bit.
Marnen is describing BigDecimal correctly:
irb> require 'bigdecimal'
=> true
irb> x = BigDecimal.new("1.50")
=> #<BigDecimal:8569c,'0.15E1',8(8)>
irb> x.to_s
=> "0.15E1"
irb> x.round(2)
=> #<BigDecimal:802b4,'0.15E1',8(16)>
irb> x.round(2).to_f
=> 1.5
irb> "%.2f"%[x.round(2)]
=> "1.50"
irb> x = BigDecimal.new("1.5431")
=> #<BigDecimal:6b0d0,'0.15431E1',8(12)>
irb> x.round(2)
=> #<BigDecimal:6845c,'0.154E1',8(16)>
irb> x.round(2).to_f
=> 1.54
irb> "%.2f"%[x.round(2)]
=> "1.54"
However, Craig, you seem to want a formatted output for your Price
model where the #price attribute happens to be a BigDecimal.
class Price
def formatted
"%.2f"%[self.price.round(2)]
end
end
Then you should have:
@price = Price.find(:first, :conditions => ["stockid = ?","F34/CW/
ES"])
@price.formatted
=> "1.50"
You might also want to roll your own helper similar to
number_to_currency
-Rob
Rob Biedenharn http://agileconsultingllc.com
[email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---