# Function for integers to displayed with same number of digit

**URL:** <https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469>\
**Category:** Ruby\
**Created:** [July 23, 2006, 1:24am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469 "2006-07-23T01:24:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [July 23, 2006, 1:24am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469/1 "2006-07-23T01:24:00Z")

</div>

Is there or a function, or anyone know of a hack without using if else  
statements extensivley to have numbers displayed, such as 1 as 0001, 100  
as 0100, 1000 as 1000 etc?

Anyhelp is appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [July 23, 2006, 2:07am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469/2 "2006-07-23T02:07:37Z")

</div>

Here’s what I’ve done. Numeric formatting for output is most natural  
with printf(): ‘printf("%d", )’, where is the number of  
digits, is the number to be displayed, and is the fill  
character. So

```
printf( "%04d", 100 )

```

yields ‘0100’, as you’re padding with zeros out to four places.

```
This is all well and good for display purposes, but what about for

```

internal use? The only solution I’ve found so far is to use the  
stringio library, which works a bit like C++'s , if you’re  
familiar with that. That is, it lets you do I/O operations on Strings.  
But it can be a bit odd until you get used to it. If, as above, you  
wanted to convert a number to a four-character string:

```
require 'stringio'
s = String.new
StringIO.new( s ).printf("%04d", 100)

```

Variable s will now hold ‘0100’. The StringIO ctor takes a reference  
to an existing String, so you can’t do the whole operation in one  
command. Note that s now functions like an output stream, repeated  
StringIO operations appending to it rather than overwriting it, and you  
must manually “flush” it with an empty-string assignment if you intend  
to reuse it as an output buffer.  
Boy, I got to use a lot of jargon in that one.

```
-Jonathan
```

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [July 23, 2006, 2:16am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469/3 "2006-07-23T02:16:54Z")

</div>

Adrian F. wrote:

> Is there or a function, or anyone know of a hack without using if else  
> statements extensivley to have numbers displayed, such as 1 as 0001, 100  
> as 0100, 1000 as 1000 etc?
> 
> Anyhelp is appreciated, thanks!

irb(main):001:0\> “%04d” % 1  
=\> “0001”

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [July 23, 2006, 2:32am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469/4 "2006-07-23T02:32:35Z")

</div>

Well, thanks a lot! Don’t I feel like a jackass now? I didn’t know  
old frog-eyes accepted format arguments, although of course it makes  
sense.

```
-J
```

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [July 23, 2006, 7:52am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469/5 "2006-07-23T07:52:47Z")

</div>

Adrian F. wrote:

> Hey, thanks to both of you for your help. I had no idea Modulus acted  
> like that.

It’s the first Instance method for the String class in the Pickaxe book.

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [July 23, 2006, 6:55am UTC](https://www.ruby-forum.com/t/function-for-integers-to-displayed-with-same-number-of-digit/99469/6 "2006-07-23T06:55:41Z")

</div>

Michael W. Ryder wrote:

> Adrian F. wrote:
> 
> > Is there or a function, or anyone know of a hack without using if else  
> > statements extensivley to have numbers displayed, such as 1 as 0001, 100  
> > as 0100, 1000 as 1000 etc?
> > 
> > Anyhelp is appreciated, thanks!
> 
> irb(main):001:0\> “%04d” % 1  
> =\> “0001”

Hey, thanks to both of you for your help. I had no idea Modulus acted  
like that.
