By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 429,301 Members | 3,575 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,301 IT Pros & Developers. It's quick & easy.

# How to get number of digits in int variable?

 P: n/a 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... Nov 14 '05 #1
Share this Question
10 Replies

 P: n/a gu*****@gmail.com 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); 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 -- E-Mail: Mine is an /at/ gmx /dot/ de address. Nov 14 '05 #2

 P: n/a Michael Mair wrote: gu*****@gmail.com 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); 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 Wouldn't that be quite slow? Why not bitshift an int of value 1? Nov 14 '05 #3

 P: n/a gu*****@gmail.com 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=1. For |n|<1, n!=0, you can use entier(-log10(|n|)) zero digits followed by S significant digits. -- SM Ryan http://www.rawbw.com/~wyrmwif/ If you plan to shoplift, let us know. Thanks Nov 14 '05 #4

 P: n/a On Thu, 19 May 2005 06:46:07 +0200, Michael Mair wrote: 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 :-) Nov 14 '05 #5

 P: n/a gu*****@gmail.com wrote: 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 */ -- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson Nov 14 '05 #6

 P: n/a wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... 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; fieldwidth = sprintf(szbuf, "%d", num_to_print); Mark Nov 14 '05 #7

 P: n/a Mark wrote: wrote: 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; 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). Nov 14 '05 #8

 P: n/a 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. Nov 14 '05 #9

 P: n/a gu*****@gmail.com 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). 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. -- Joe Wright mailto:jo********@comcast.net "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Nov 14 '05 #10

 P: n/a gu*****@gmail.com 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). 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 | +----------------------------------------------------------------+ Nov 14 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion. 