Forum: Ruby printf inside a string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-03-01 16:39
Hi,
This is a pretty simple question. I need to do a "printf" of some
numbers inside a string. I just can't figure out how to express a long
string that has that "printf" inside it, because the printf statement
itself has quotes, too. I just need the number to end up being
zero-padded to 3 characters. So, for "3," I want "003."

number = "3"

puts "stuff, printf "%.3d\n", #{number}"
      ...compile error...

Thanks,
Peter
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-03-01 16:45
(Received via mailing list)
On Mar 1, 2007, at 9:39 AM, Peter Bailey wrote:

> Hi,
> This is a pretty simple question. I need to do a "printf" of some
> numbers inside a string. I just can't figure out how to express a long
> string that has that "printf" inside it, because the printf statement
> itself has quotes, too. I just need the number to end up being
> zero-padded to 3 characters. So, for "3," I want "003."
>
> number = "3"
>
> puts "stuff, printf "%.3d\n", #{number}"

 >> printf "stuff, %.3d\n", number
stuff, 003
=> nil

Hope that helps.

James Edward Gray II
93697354fea1f7bae30d3ab89917c4c3?d=identicon&s=25 Alin Popa (alin_popa)
on 2007-03-01 16:50
Peter Bailey wrote:
> Hi,
> This is a pretty simple question. I need to do a "printf" of some
> numbers inside a string. I just can't figure out how to express a long
> string that has that "printf" inside it, because the printf statement
> itself has quotes, too. I just need the number to end up being
> zero-padded to 3 characters. So, for "3," I want "003."
>
> number = "3"
>
> puts "stuff, printf "%.3d\n", #{number}"
>       ...compile error...
>
> Thanks,
> Peter

Hi Peter,

I don't quite get what are you trying to do, but I advice to do that:

printf("stuff, %.3d\n",number)

That should be all.

Alin.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2007-03-01 17:07
(Received via mailing list)
On Fri, 2 Mar 2007, James Edward Gray II wrote:

>>
>> puts "stuff, printf "%.3d\n", #{number}"
>
>>> printf "stuff, %.3d\n", number
> stuff, 003
> => nil
>
> Hope that helps.

alternatively

puts "stuff, #{ '%.3d' % number }"


-a
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-03-01 17:17
(Received via mailing list)
On Fri, Mar 02, 2007 at 12:39:13AM +0900, Peter Bailey wrote:
> This is a pretty simple question. I need to do a "printf" of some
> numbers inside a string. I just can't figure out how to express a long
> string that has that "printf" inside it, because the printf statement
> itself has quotes, too.

I think what you were trying to do is:

  number = "3"
  puts "stuff, #{sprintf "%.3d", number}"

Note that sprintf(..) is like printf(..) but returns the result as a
string
value, rather than sending it to stdout. Then #{} lets you insert an
arbitary expression inside another string.

But as pointed out by others, you can do this in one go as

  printf "stuff, %.3d\n", number

HTH,

Brian.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2007-03-01 18:00
Brian Candler wrote:
> On Fri, Mar 02, 2007 at 12:39:13AM +0900, Peter Bailey wrote:
>> This is a pretty simple question. I need to do a "printf" of some
>> numbers inside a string. I just can't figure out how to express a long
>> string that has that "printf" inside it, because the printf statement
>> itself has quotes, too.
>
> I think what you were trying to do is:
>
>   number = "3"
>   puts "stuff, #{sprintf "%.3d", number}"
>
> Note that sprintf(..) is like printf(..) but returns the result as a
> string
> value, rather than sending it to stdout. Then #{} lets you insert an
> arbitary expression inside another string.
>
> But as pointed out by others, you can do this in one go as
>
>   printf "stuff, %.3d\n", number
>
> HTH,
>
> Brian.

Thanks to all you guys. I'll probably use sprintf, actually, now that
I'm clear about how it's used.

Cheers.
This topic is locked and can not be replied to.