[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Add a zero on single digit numbers

mind2motion

11/19/2006 12:59:00 AM

Using DateTime.now.min I often get single digit numbers like "7".
However "22:7" isn't really adequate for displaying time properly; I
want "22:07".

Is there any way add a 0 onto the front of a Fixnum?

I understand I could convert it into a string and add a zero onto the
front of that string, but that's not really ideal.

2 Answers

Tim Hunter

11/19/2006 1:11:00 AM

0

mind2motion@gmail.com wrote:
> Using DateTime.now.min I often get single digit numbers like "7".
> However "22:7" isn't really adequate for displaying time properly; I
> want "22:07".
>
> Is there any way add a 0 onto the front of a Fixnum?
>
> I understand I could convert it into a string and add a zero onto the
> front of that string, but that's not really ideal.
>
>
check out DateTime#strftime.


Ben Bleything

11/19/2006 1:57:00 AM

0

On Sun, Nov 19, 2006, mind2motion@gmail.com wrote:
> Is there any way add a 0 onto the front of a Fixnum?

Sure:

num = 7
printf('%02d', num)

You can use #sprintf instead if you want to assign that back to a
variable.

That said, Timothy's suggestion of using #strftime is a much better
solution to your problem... it's just valuable to know about #(s)printf
in general.

Ben