# how to know how many digits

Page 1 of 1

## 7 Replies - 212 Views - Last Post: 05 October 2012 - 09:43 PMRate 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=294456&amp;s=cf22130f456a6ae43ff7389cdb839250&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 exfoxs28

Reputation: 0
• Posts: 15
• Joined: 25-August 12

# how to know how many digits

Posted 05 October 2012 - 04:49 PM

I input 2 numbers and add it ..
my problem is how to know how many digits of number is in the sum??
Is This A Good Question/Topic? 0

## Replies To: how to know how many digits

### #2 Skydiver

• Code herder

Reputation: 1929
• Posts: 5,743
• Joined: 05-May 12

## Re: how to know how many digits

Posted 05 October 2012 - 04:51 PM

What have you tried so far?

Are the numbers integers, or are they floating point numbers, or are they complex numbers?

And do you have to follow any scientific or accounting conventions like if you add "1.75" and "0.25", the answer should be written as "2.00" and not just "2".

This post has been edited by Skydiver: 05 October 2012 - 04:53 PM

### #3 exfoxs28

Reputation: 0
• Posts: 15
• Joined: 25-August 12

## Re: how to know how many digits

Posted 05 October 2012 - 04:53 PM

the numbers is integers...
sorry but i havent try it yet..

### #4 Skydiver

• Code herder

Reputation: 1929
• Posts: 5,743
• Joined: 05-May 12

## Re: how to know how many digits

Posted 05 October 2012 - 04:59 PM

Do you have to know the exact number of digits, or is a rough estimate of the minimum and maximum number of digits good enough?

Or phrased another way: are you allowed to add the two numbers first and determine the number of digits, or do you have to know the number of digits of the result without actually performing the addition?

And last question, we are talking about decimal digits, right? Not binary digits, or hexadecimal digits.

### #5 exfoxs28

Reputation: 0
• Posts: 15
• Joined: 25-August 12

## Re: how to know how many digits

Posted 05 October 2012 - 05:04 PM

i have to know the exact number of digits

yup add the number first then determine the number of digits

yes were talking about decimal digits..

### #6 Skydiver

• Code herder

Reputation: 1929
• Posts: 5,743
• Joined: 05-May 12

## Re: how to know how many digits

Posted 05 October 2012 - 06:44 PM

So you said that you haven't tried anything yet. Can you describe how you would approach this problem if you had to do it with pen and paper?

### #7 #define

• Duke of Err

Reputation: 979
• Posts: 3,397
• Joined: 19-February 09

## Re: how to know how many digits

Posted 05 October 2012 - 08:26 PM

One method is to repeatedly divide the number by 10 until it reaches 0 and count the number of divide operations. Another is to convert to a string and check the length.

### #8 Skydiver

• Code herder

Reputation: 1929
• Posts: 5,743
• Joined: 05-May 12

## Re: how to know how many digits

Posted 05 October 2012 - 09:43 PM

Or convert to string using sprintf() and let sprintf() do the counting for you.