...
I changed the test-program like this:
require 'bigdecimal'
print("Length: ")
s = gets()
length = BigDecimal.new(s)
print("Width: ")
s = gets()
width = BigDecimal.new(s)
surface = length * width
puts("Surface = #{length.to_f} x #{width.to_f} = #{surface.to_f}")
It works fine now, but, well... I don't think it's very 'elegant'...
I haven't had the time yet to read the page on BigDecimal, but I'll do
that right now.
I believe there is no need to convert to float (with to_f).
A problem that I did face is that the default to_s on BigDecimal
gives engineering notation ( the #{length} will execute length.to_s ).
From:
to_s(s) click to toggle source
Converts the value to a string.
The default format looks like 0.xxxxEnn.
I find that non-optimal. I see BigDecimal used mainly for business
calculations
(money, sizes, amounts of goods etc.) and there the 'g/G' format specifier
seems most suited as default ...
If the Float (or G) notation was the default, you could simply write your
original code and it would do the
puts("Surface = #{length} x #{width} = #{surface}")
For reference, the "g/G" format specifier (e.g. in C printf):
g, G double in either normal or exponential notation, whichever is more
appropriate for its magnitude. 'g' uses lower-case letters, 'G' uses
upper-case letters. This type differs slightly from fixed-point notation in
that insignificant zeroes to the right of the decimal point are not
included. Also, the decimal point is not included on whole numbers.
HTH,
Peter
···
On Fri, Dec 16, 2011 at 2:22 PM, Jan Hendrickx <jan@vlaamsemolshoop.be>wrote:
from: printf - Wikipedia
--
Peter Vandenabeele
http://twitter.com/peter_v
http://rails.vandenabeele.com