cannot convert Float into String

N

Nick

i am making a pdf file, retrieving an product for the db and print the price
in the pdf

@product = Product.find(@params["id"])

pdf.SetFont('Arial','',10)
pdf.Cell(3,h, "?",0,0)
pdf.Cell(100,h, " " + @product.price , 0,1)


error : cannot convert Float into String

any help
 
R

Robert Klemme

Nick said:
i am making a pdf file, retrieving an product for the db and print
the price in the pdf

@product = Product.find(@params["id"])

pdf.SetFont('Arial','',10)
pdf.Cell(3,h, "?",0,0)
pdf.Cell(100,h, " " + @product.price , 0,1)

pdf.Cell(100,h, @product.price.to_s , 0,1)
pdf.Cell(100,h, " %4.1f" % @product.price , 0,1)
pdf.Cell(100,h, sprintf(" %4.1f", @product.price) , 0,1)

Depending on what you need...

robert
 
D

Devin Mullins

Robert said:
Nick said:
i am making a pdf file, retrieving an product for the db and print
the price in the pdf

@product = Product.find(@params["id"])

pdf.SetFont('Arial','',10)
pdf.Cell(3,h, "?",0,0)
pdf.Cell(100,h, " " + @product.price , 0,1)


pdf.Cell(100,h, @product.price.to_s , 0,1)
pdf.Cell(100,h, " %4.1f" % @product.price , 0,1)
pdf.Cell(100,h, sprintf(" %4.1f", @product.price) , 0,1)

Add to that:
pdf.Cell(100,h, "#{@product.price}", 0,1)

The technical explanation is that Ruby does not automatically cast
between basic Classes. For that matter, since types are never declared,
Ruby has no way of knowing which Class you want. So you tell it which
Class you want, by calling #to_s on the Object, which asks the (Float in
this case), "I want a String representation of you."

Some builtin methods (such as #puts) will automatically call #to_s on
the object to get its String representation, though. Every Object has a
#to_s method, which, by default, returns something like
"#<Object:0xfed89350". Some Objects, such as String and Float, override
this method with their own version. (String#to_s returns self.)

If you got through all that, then you'll be fine. :) If you didn't, I'll
be happy to expound.

Devin
 
R

Robert Klemme

Devin Mullins said:
Robert said:
Nick said:
i am making a pdf file, retrieving an product for the db and print
the price in the pdf

@product = Product.find(@params["id"])

pdf.SetFont('Arial','',10)
pdf.Cell(3,h, "?",0,0)
pdf.Cell(100,h, " " + @product.price , 0,1)


pdf.Cell(100,h, @product.price.to_s , 0,1)
pdf.Cell(100,h, " %4.1f" % @product.price , 0,1)
pdf.Cell(100,h, sprintf(" %4.1f", @product.price) , 0,1)

Add to that:
pdf.Cell(100,h, "#{@product.price}", 0,1)

Although I agree to the rest of your posting this variant is inefficient if
you just want to convert a float into a string.

$ ruby -r benchmark -e 'x=1.34;Benchmark.bm(20) {|bm| bm.report("to_s")
{10000.times{ x.to_s }}; bm.report("##"){10000.times{"#{x}"
}} }'
user system total real
to_s 0.438000 0.000000 0.438000 ( 0.430000)
## 0.500000 0.000000 0.500000 ( 0.491000)

Kind regards

robert
 
D

Devin Mullins

Robert said:
Although I agree to the rest of your posting this variant is
inefficient if you just want to convert a float into a string.

I'll buy that. It is, however, more convenient in a longer string such
as: "The price is #{@product.price}. Do you want to pay?" Sorry if my
previous post was misleading in that respect.

Depending on what you need... :)

Devin
 

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
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top