# Number of characters in an int value

Page 1 of 1

## 6 Replies - 2138 Views - Last Post: 11 February 2008 - 10:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42839&amp;s=1f5724b58e8af7032fadff44498d10ef&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 thepeon

• New D.I.C Head

Reputation: 0
• Posts: 47
• Joined: 04-February 08

# Number of characters in an int value

Posted 09 February 2008 - 11:11 AM

I need to know how to output the number of characters in an int or double value. I know for a string you can use s.length. Is there a similar command for integer values?

example

55 would return 2
5628 would return 4
9 would return 1

Thanks for your help
Is This A Good Question/Topic? 0

## Replies To: Number of characters in an int value

### #2 schnalf

Reputation: 6
• Posts: 129
• Joined: 09-February 08

## Re: Number of characters in an int value

Posted 09 February 2008 - 11:28 AM

i don't understand what you exactly mean.
the size is mostly 2 or 4 bytes, which you can check with sizeof()

or what do you mean?

### #3 skaoth

Reputation: 91
• Posts: 601
• Joined: 07-November 07

## Re: Number of characters in an int value

Posted 09 February 2008 - 12:21 PM

There are functions to convert ascii to integers and integers to ascii.
These are atoi() and itoa() respectively.

What you can do is to convert the integer into a string
and use the std::string length function to give you the length.

```	int a = 4444;
char buff[256] = {0};
itoa(a, buff, 10);
std::cout << "Int is: " << a << " len is :" << strlen(buff) << std::endl;

```

### #4 thepeon

• New D.I.C Head

Reputation: 0
• Posts: 47
• Joined: 04-February 08

## Re: Number of characters in an int value

Posted 09 February 2008 - 02:36 PM

skaoth, on 9 Feb, 2008 - 12:21 PM, said:

There are functions to convert ascii to integers and integers to ascii.
These are atoi() and itoa() respectively.

What you can do is to convert the integer into a string
and use the std::string length function to give you the length.

```	int a = 4444;
char buff[256] = {0};
itoa(a, buff, 10);
std::cout << "Int is: " << a << " len is :" << strlen(buff) << std::endl;

```

Thanks

### #5 Bench

• D.I.C Lover

Reputation: 938
• Posts: 2,462
• Joined: 20-August 07

## Re: Number of characters in an int value

Posted 09 February 2008 - 02:50 PM

thepeon, on 9 Feb, 2008 - 06:11 PM, said:

I need to know how to output the number of characters in an int or double value. I know for a string you can use s.length. Is there a similar command for integer values?

example

55 would return 2
5628 would return 4
9 would return 1

Thanks for your help

you can convert any numeric value to a string in C++ using stringstreams - this will allow you to use the length() function.
```#include <iostream>
#include <string>
#include <sstream>

int main()
{
int i = 55555;
std::stringstream ss;
ss << i;
std::string num = ss.str();
std::cout << num.length();
}
```

### #6 baavgai

• Dreaming Coder

Reputation: 6574
• Posts: 13,906
• Joined: 16-October 07

## Re: Number of characters in an int value

Posted 09 February 2008 - 04:41 PM

As previously stated, you can convert the number to a string and ask the string functions for a length. It don't wish to invoke such a library, this approach should also work.

```int getNumSize(const int n) {
if (n==0) { return 0; }
return 1 + getNumSize(n/10);
}

cout << getNumSize(4321) << endl;

```

### #7 venke.mv

• New D.I.C Head

Reputation: 1
• Posts: 14
• Joined: 22-December 07

## Re: Number of characters in an int value

Posted 11 February 2008 - 10:43 AM

you can use this coding in C to get the no. of digits in an integer.

#include <stdio.h>
#include <conio.h>
void main()
{
int n,d,sum=0;
clrscr();
printf("\nEnter An Integer: ");
scanf("%d",&n);
while(n>0)
{
d=n%10;
++sum;
n=n/10;
}
printf("The sum of the digits is %d",sum);
getch();
}