How to get number of digits in int variable?

G

guidosh

Hello,

I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:

printf("%*", fieldwidth, num_to_print);

However, I can't figure out how to get the number of digits in the
number (which I would then assign to the 'fieldwidth' variable above).

I know I don't have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?

Thanks...
 
M

Michael Mair

Hello,

I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:

printf("%*", fieldwidth, num_to_print);

Which will not do anything useful.
ITYM "%*d" or whatever format specifier is appropriate for
num_to_print.
However, I can't figure out how to get the number of digits in the
number (which I would then assign to the 'fieldwidth' variable above).

If num_to_print is of type int, you could use
int tmp = num_print;

for (fieldwidth = 0; tmp != 0; tmp /= 10)
fieldwidth += 1;
if (num_print < 0)
fieldwidth += 1;
(untested)
Otherwise you want
fieldwidth = (int)ceil(log(fabs(num_print))/log(10)) + (num_print < 0);
which is quite a lot of work for this information.
Alternatively, I would just use 1 + (CHAR_BIT*sizeof num_print)/3
as an overestimation to get an always wide enough field.
I know I don't have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?

Note that the above is about integer types.
Floating point types
- tell you how many (valid) decimal digits they have
- and usually should not printed to their full precision
if not necessary

Cheers
Michael
 
A

Arafangion

Michael said:
Which will not do anything useful.
ITYM "%*d" or whatever format specifier is appropriate for
num_to_print.



If num_to_print is of type int, you could use
int tmp = num_print;

for (fieldwidth = 0; tmp != 0; tmp /= 10)
fieldwidth += 1;
if (num_print < 0)
fieldwidth += 1;
(untested)
Otherwise you want
fieldwidth = (int)ceil(log(fabs(num_print))/log(10)) + (num_print < 0);
which is quite a lot of work for this information.
Alternatively, I would just use 1 + (CHAR_BIT*sizeof num_print)/3
as an overestimation to get an always wide enough field.



Note that the above is about integer types.
Floating point types
- tell you how many (valid) decimal digits they have
- and usually should not printed to their full precision
if not necessary

Cheers
Michael
Wouldn't that be quite slow? Why not bitshift an int of value 1?
 
S

SM Ryan

(e-mail address removed) wrote:
# Hello,
#
# I'm trying to write a printf statement that sets the field width when
# printing a number. I'm using this:
#
# printf("%*", fieldwidth, num_to_print);
#
# However, I can't figure out how to get the number of digits in the
# number (which I would then assign to the 'fieldwidth' variable above).

L = entier(log10(|n|))+1, |n|>=1, is the number of digits left of the decimal point.
For -1<n<1, it is customary to present L=1 digit ('0') left of the decimal point.
If n<0, it is customary to include a negative mark such as '-'.

All reals have an infinite repeating or nonrepeating decimal representation,
so that right of the decimal point there's no specific limit. For integers, you
can use 0. For floats, if you can get the approximate number of significant
digits supported by the hardware, S, you can use R = S-L as the number of digits
to the right for |n|>=1. For |n|<1, n!=0, you can use entier(-log10(|n|))
zero digits followed by S significant digits.
 
P

Paul Mesken

If num_to_print is of type int, you could use
int tmp = num_print;

for (fieldwidth = 0; tmp != 0; tmp /= 10)
fieldwidth += 1;
if (num_print < 0)
fieldwidth += 1;
(untested)

Then test it with tmp = 0 :)

A multiplication would be faster than a division and doesn't really
change the readability of the code (shifts would and multiplications
are so fast nowadays that it doesn't really pay to use them instead of
mulitplications when more than 1 shift is needed).

int tmp = num_print, fieldwidth = 1, testvalue = 10;

if (tmp < 0)
{
++ fieldwidth;
tmp *= -1;
}
if (tmp > SomeOverflowValue)
fieldwidth += TheNumberOfDigitsOfThatOverflowValueMinus1
else
while(tmp >= testvalue)
{
++ fieldwidth;
testvalue *= 10;
}

For a 32 bit integer "SomeOverflowValue" would be 1 billion (we don't
want undefined behaviour which would probably result in an endless
loop).

Of course, a far quicker solution would involve a lot of if's.
Something like this (you'll need to know the maximum value an int can
have as defined in limits.h) :

if (tmp < 10000)
if (tmp < 100)
if (tmp < 10)
fieldwidth += 1;
else
fieldwidth += 2;
else if (tmp < 1000)
fieldwidth += 3;
else
fieldwidth += 4;
else if (tmp < 10000000)

etc.

The basic idea is that the number of executed if's will be minimized
by using some binary tree like structure (checking first for the
situation with half the maximum number of digits, etc.).

I'm sure there must be better ways but, unfortunately, I'm not
mathematically endowed :)
 
C

CBFalconer

I'm trying to write a printf statement that sets the field width
when printing a number. I'm using this:

printf("%*", fieldwidth, num_to_print);

However, I can't figure out how to get the number of digits in
the number (which I would then assign to the 'fieldwidth' variable
above).

I know I don't have to explicitly set the field width with
printf, but I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a
numeric variable?

The following routine may be useful. They have been designed to
avoid the heavy overhead of a complete printf system.


/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
static int hexify(unsigned int value)
{
static char hexchars[] = "0123456789abcdef";

return (hexchars[value & 0xf]);
} /* hexify */

/* ----------------------- */

/* Convert value to stream of digits
A NULL value for f returns a char count with no output
Returns count of chars. output
return is negative for any output error */
int unum2txt(unsigned long n, int base, FILE *f)
{
int count, err;

if ((base < 2) || (base > 16)) base = 10;
count = 1;
if (n / base) {
if ((err = unum2txt(n / base, base, f)) < 0) return err;
else count += err;
}
if (f && (putc(hexify(n % base), f) < 0)) return -count;
return count;
} /* unum2txt */
 
M

Mark

Hello,
I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:
printf("%*", fieldwidth, num_to_print);
format specification is missing type...
However, I can't figure out how to get the number of digits in the
number (which I would then assign to the 'fieldwidth' variable above).
I know I don't have to explicitly set the field width with printf, but
I would like very much to know how to discover it.
Can anyone suggest how I might get the number of digits for a numeric
variable?

sprintf();
....
char szbuf[40];
fieldwidth = sprintf(szbuf, "%d", num_to_print);

Mark
 
O

Old Wolf

Mark said:
I can't figure out how to get the number of digits in the number
Can anyone suggest how I might get the number of digits for a numeric
variable?

sprintf();
...
char szbuf[40];
fieldwidth = sprintf(szbuf, "%d", num_to_print);

I'm getting slightly pedantic here, but on a system with 128-bit int
(or larger), this may cause a buffer overflow.
You could either use the preprocessor to determine the maximum
buffer size required, or if using C99, snprintf(0, 0, "%d", num).
 
G

guidosh

Hi folks,

Thanks muchly for your ideas. Mark's idea of using sprintf seems a good
one, so I'll try that. Old Wolf, thanks for the note about buffer
overflows too...

G.
 
J

Joe Wright

Hello,

I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:

printf("%*", fieldwidth, num_to_print);

However, I can't figure out how to get the number of digits in the
number (which I would then assign to the 'fieldwidth' variable above).

I know I don't have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?

Thanks...

I realize I'm late responding here but first, I don't see your problem.

printf("%d\n", num_to_print);

...will print the 'right' number of digits left-justified.

12
3456
7
876

I propose that fieldwidth is not calculated, but defined by the format.
The 'f' in printf is for format.

This allows right-justifying a column of numbers..

int fieldwidth = 5;
printf("%*d\n", fieldwidth, num_to_print);

12
3456
7
876

Of course, I might have missed the whole point.
 
C

Charles Richmond

Hello,

I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:

printf("%*", fieldwidth, num_to_print);

However, I can't figure out how to get the number of digits in the
number (which I would then assign to the 'fieldwidth' variable above).

I know I don't have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?
The integer part of the log base 10 of MAXINT will tell you how
many digits are maximum in an integer. Add one more for a negative
sign.

If you know the number of bits representing a number (*not* counting
the sign bit), divide this by approximately 3.32 (1 / log 2) to get
the number of decimal digits that can be represented. The fractional
part (if non-zero) means that one more digit can represent numbers
somewhat less than "9".

--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top