[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

printf inside a string

Peter Bailey

3/1/2007 3:39:00 PM

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

--
Posted via http://www.ruby-....

6 Answers

James Gray

3/1/2007 3:45:00 PM

0

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

Alin Popa

3/1/2007 3:51:00 PM

0

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.

--
Posted via http://www.ruby-....

Oliver Vecernik

3/1/2007 3:51:00 PM

0

Peter Bailey wrote:
> 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"

--
Cheers,
Oliver

Ara.T.Howard

3/1/2007 4:07:00 PM

0

Brian Candler

3/1/2007 4:18:00 PM

0

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.

Peter Bailey

3/1/2007 5:00:00 PM

0

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.


--
Posted via http://www.ruby-....