--20cf30334e5ba1c7ff04b4366db4
Content-Type: text/plain; charset=UTF-8

On Fri, Dec 16, 2011 at 2:22 PM, Jan Hendrickx <jan / vlaamsemolshoop.be>wrote:
...

> I changed the test-program like this:
>
> require 'bigdecimal'
> print("Length: ")
> s  ets()
> length  igDecimal.new(s)
> print("Width: ")
> s  ets()
> width  igDecimal.new(s)
> surface  ength * 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:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html#method-i-to_s

  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):

from: http://en.wikipedia.org/wiki/Printf_format_string

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

-- 
Peter Vandenabeele
http://twitter.com/peter_v
http://rails.vandenabeele.com

--20cf30334e5ba1c7ff04b4366db4--