Cannot convert Float into String

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

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

···

Nick <glenn.brutyn@telenet.be> wrote:

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)

Robert Klemme wrote:

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. :slight_smile: If you didn't, I'll be happy to expound.

Devin

···

Nick <glenn.brutyn@telenet.be> wrote:

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

···

Devin Mullins <twifkak@comcast.net> wrote:

Robert Klemme wrote:

Nick <glenn.brutyn@telenet.be> wrote:

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)

Robert Klemme wrote:

···

Devin Mullins <twifkak@comcast.net> wrote:

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.

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... :slight_smile:

Devin