Product Class #2

I was wondering if anyone had any ideas on how I could make the following
method "return "$#{@price}0" if "@price" only has one 0 after the ".", and
just "return "$#{@price}" if it has two?
Thanks.

(The following method is inside a class called "Product"... Hence the
subject)

    def price
        if @price.to_s.length == 4
            return "$#{@price}"
        else @price.to_s.length
            return "$#{@price}0"
        end
    end

"$%.2f" % @price

Note though that for accuracy reason it's best to store currency as an int of
cents, not a float of dollars so then it would look like this:
"$%d.%02d" % @price.divmod(100)

HTH,
Sebastian

···

Am Montag 22 Juni 2009 01:43:35 schrieb Zorigami:

was wondering if anyone had any ideas on how I could make the following
method "return "$#{@price}0" if "@price" only has one 0 after the ".", and
just "return "$#{@price}" if it has two?

Thanks, that helped

···

On Sun, Jun 21, 2009 at 7:55 PM, Sebastian Hungerecker < sepp2k@googlemail.com> wrote:

Am Montag 22 Juni 2009 01:43:35 schrieb Zorigami:
> was wondering if anyone had any ideas on how I could make the following
> method "return "$#{@price}0" if "@price" only has one 0 after the ".",
and
> just "return "$#{@price}" if it has two?

"$%.2f" % @price

Note though that for accuracy reason it's best to store currency as an int
of
cents, not a float of dollars so then it would look like this:
"$%d.%02d" % @price.divmod(100)

HTH,
Sebastian

Sorry, I'm a beginner at Ruby(Not at programming though...)
Could you show me that in the method?
Thanks.

···

On Sun, Jun 21, 2009 at 7:55 PM, Sebastian Hungerecker < sepp2k@googlemail.com> wrote:

Am Montag 22 Juni 2009 01:43:35 schrieb Zorigami:
> was wondering if anyone had any ideas on how I could make the following
> method "return "$#{@price}0" if "@price" only has one 0 after the ".",
and
> just "return "$#{@price}" if it has two?

"$%.2f" % @price

Note though that for accuracy reason it's best to store currency as an int
of
cents, not a float of dollars so then it would look like this:
"$%d.%02d" % @price.divmod(100)

HTH,
Sebastian

In both cases you can turn the snippet into a method by putting "def foo()" in
front of it and "end" behind it. That's all there is to it.

HTH,
Sebastian

···

Am Montag 22 Juni 2009 03:50:31 schrieb Zorigami:

[I wrote:]
> "$%.2f" % @price
>
> Note though that for accuracy reason it's best to store currency as an
> int of
> cents, not a float of dollars so then it would look like this:
> "$%d.%02d" % @price.divmod(100)

Sorry, I'm a beginner at Ruby(Not at programming though...)
Could you show me that in the method?
Thanks.