# Product Class #2

**URL:** <https://www.ruby-forum.com/t/product-class-2/170726>\
**Category:** Ruby\
**Created:** [June 22, 2009, 1:44am UTC](https://www.ruby-forum.com/t/product-class-2/170726 "2009-06-22T01:44:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zorigami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ee7513/32.png) [@Zorigami](https://www.ruby-forum.com/u/Zorigami)\
**Post date:** [June 22, 2009, 1:44am UTC](https://www.ruby-forum.com/t/product-class-2/170726/1 "2009-06-22T01:44:00Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Zorigami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ee7513/32.png) [@Zorigami](https://www.ruby-forum.com/u/Zorigami)\
**Post date:** [June 22, 2009, 1:55am UTC](https://www.ruby-forum.com/t/product-class-2/170726/2 "2009-06-22T01:55:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zorigami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ee7513/32.png) [@Zorigami](https://www.ruby-forum.com/u/Zorigami)\
**Post date:** [June 22, 2009, 3:24am UTC](https://www.ruby-forum.com/t/product-class-2/170726/3 "2009-06-22T03:24:31Z")

</div>

Thanks, that helped

On Sun, Jun 21, 2009 at 7:55 PM, Sebastian H. \<

---

<div class="post-metadata">

**Author:** ![Zorigami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ee7513/32.png) [@Zorigami](https://www.ruby-forum.com/u/Zorigami)\
**Post date:** [June 22, 2009, 3:50am UTC](https://www.ruby-forum.com/t/product-class-2/170726/4 "2009-06-22T03:50:57Z")

</div>

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 H. \<

---

<div class="post-metadata">

**Author:** ![Zorigami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ee7513/32.png) [@Zorigami](https://www.ruby-forum.com/u/Zorigami)\
**Post date:** [June 24, 2009, 1:13am UTC](https://www.ruby-forum.com/t/product-class-2/170726/5 "2009-06-24T01:13:27Z")

</div>

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

> Thanks.  
> 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
