6 Replies - 1400 Views - Last Post: 11 February 2008 - 10:43 AM Rate Topic: -----

#1 thepeon  Icon User is offline

  • New D.I.C Head
  • member icon

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

Number of characters in an int value

Post icon  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  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

#4 thepeon  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: Number of characters in an int value

Posted 09 February 2008 - 02:36 PM

View Postskaoth, 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
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Number of characters in an int value

Posted 09 February 2008 - 02:50 PM

View Postthepeon, 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();
} 

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • 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;


Was This Post Helpful? 0
  • +
  • -

#7 venke.mv  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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();
}
Was This Post Helpful? 1

Page 1 of 1