473,795 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get number of digits in int variable?

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
10 4246
gu*****@gmail.c om 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1474
by: Philippe Rousselot | last post by:
Hi, On a page, I have a form with a variable number of fields. to print these fields, I have such a code : while ($i !=0){ echo '<td> <input type="text" name="component'.$i.'" size="20" value="" maxlength="70"> </td><br> <td> <input type="text" name="quantity'.$i.'" size="20" value="" maxlength="70"><br>
10
2334
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example: pi = 3.14159... mynum = 3.14226... The mynum is accurate to 2 decimal places compared to pi. I could figure this out easy with a char array since I could just take a character one at a time from each array and compare it. However with a...
11
3144
by: abs | last post by:
Input field of the form. How to check which number this field has ? I mean the order number, like variable elementNumber in document.forms.elements ? Is it possible to check without looping and comparing some attributes in every loop ? Best regards, ABS
2
1843
by: Jon Martin Solaas | last post by:
Hi, I have a general document somewhat like this: -------------------------------------- <root> <level1> <level2> <interestingstuff number="2"/> <interestingstuff number="3"/> <interestingstuff number="1"/>
6
1562
by: ven | last post by:
hello i want to get current record in MS SQL table with identity column, let say i put somethin using INSERT INTO and then i want to know current number in identity column how can i put this number into variable in vbscript ? Ven
6
7362
by: Jovo Mirkovic | last post by:
Hi, I have to make a program which will generate 100,000 different ID numbers (for example 2345-9341-0903-3432-3432 ...) It must be really different, I meen it can not be a similar (like 2345-9341-0903-3432-343<b>1</b>) Does exist some algorithm for that... Thanks.
14
1455
by: Gary | last post by:
Hi, I am trying to format a number to the following, "10" to "10." using, Printline(1, format(10, "###.###)) naturally this does not achieve the desired result, I know if I do this, Printline(1, format(10, "###.0##)) I get this "10.0" but I do not need the trailing "0" any ideas on how to get from this "10" to this "10."? Thanks,
4
4218
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I am trying to convert the digits to even number so for example if I have 3 digit number, I want it to be 4 digit and If I have 5 digit number, I want it to be 6 digit. How can i do it in C# Below is what I am trying to di number = 123 I want it to be
0
1237
by: John Doe77 | last post by:
Hi, Given a phone number I need to print out all the word representation combinations possible from that phone number. Digits translate into chars like the following: 1 = 1 2 = A B C 3 = D E F 4 = G H I 5 = J K L 6 = M N O
2
9757
by: jeanbdenis | last post by:
Hi Folks, I have been struggling with this issues for the last couple of days. I have a java application which does an update to the database every 5 mins. The data written to the database does not change much from one minute to the next. Just the timestamp. While this application runs 24/7, the error occurs once or twice a day (It's an intermittent problem which I have not been able to replicate in my dev environment) below is the...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.