printf inside a string

P

Peter Bailey

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
 
J

James Edward Gray II

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}"
stuff, 003
=> nil

Hope that helps.

James Edward Gray II
 
A

Alin Popa

Peter said:
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.
 
O

Oliver VeÄernik

Peter said:
number = "3"

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

irb(main):001:0> number="3"
=> "3"
irb(main):002:0> printf "stuff, %.3d\n", number
stuff, 003
=> nil
irb(main):003:0> ri "Kernel#sprintf"
 
B

Brian Candler

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.
 
P

Peter Bailey

Brian said:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top