Re: Getting the units of a number

From: Peter van Merkerk (merkerk_at_deadspam.com)
Date: 10/23/03


Date: Thu, 23 Oct 2003 14:57:42 +0200


> I need to get the units of a number.
>
> i.e. if I had 12345 then I would want '5'.
>
> I was looking at dividing the number by ten and using the modulus
> operator repeatedly until I had only the units, but wondered if there
> was an easier way?

The loop could do without the modulus operator:

   int number = ....
   int units = 0;
   while(number)
   {
      units++;
      number/=10;
   }

One other way to do it would be to use log10():

    int units = log10(number) + 1; // number must be > 0

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Relevant Pages

  • Re: fft phase for object detection
    ... dividing each value by its modulus (thus removing ... coefficients from polar back to Cartesian form, ...
    (sci.image.processing)
  • Re: help solving addition problem in C
    ... take a temporary variable to store sum and initialize it to zero, ... Now by recursively taking modulus and dividing the number you can each ...
    (comp.lang.c)
  • Re: Getting the units of a number
    ... "Adrian Gibbons" wrote in message ... > I was looking at dividing the number by ten and using the modulus ... Or did you mean the number of digits in the integer? ...
    (comp.lang.cpp)
  • Getting the units of a number
    ... I was looking at dividing the number by ten and using the modulus ... Regards, ... Adrian. ...
    (comp.lang.cpp)