Number of numbers in an int

This is a discussion on Number of numbers in an int within the C++ Programming forums, part of the General Programming Boards category; How can i figure out the number of numbers in an int value IE 12345 has 5 numbers in it. ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Number of numbers in an int

    How can i figure out the number of numbers in an int value
    IE
    12345 has 5 numbers in it.
    Need this beacause i'm making a simple method to convort a int to a string;

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Repeat division (with truncation) by 10 until you get 0.

    12345 / 10 = 1234
    1234 / 10 = 123
    123 / 10 = 12
    12 / 10 = 1
    1 / 10 = 0

    5 divisions => 5 digits
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Thanks. i shall put it to good use. loop it till it equals 0

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You can also take the base 10 log and round to the next highest integer (actually, take the log, cast to an int, and add 1). That would probably be faster.

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    will try that. got some code almost work using this method so i might just stick to this.. although speed is always better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21