From: Devin Mullins Date: 2005-09-25T01:33:25+09:00 Subject: Re: cannot convert Float into String Robert Klemme wrote: > Nick 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 "#