Product Class #2

Z

Zorigami

[Note: parts of this message were removed to make it a legal post.]

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
 
S

Sebastian Hungerecker

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
 
Z

Zorigami

[Note: parts of this message were removed to make it a legal post.]

Thanks, that helped
 
Z

Zorigami

[Note: parts of this message were removed to make it a legal post.]

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

Sebastian Hungerecker

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top