Re: Getting the units of a number
From: Peter van Merkerk (merkerk_at_deadspam.com)
Date: 10/23/03
- Next message: Jerry Coffin: "Re: Addition of strings in C++"
- Previous message: Tim Clacy: "class static variables & __STDC_VERSION__"
- In reply to: Adrian Gibbons: "Getting the units of a number"
- Next in thread: Artie Gold: "Re: Getting the units of a number"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jerry Coffin: "Re: Addition of strings in C++"
- Previous message: Tim Clacy: "class static variables & __STDC_VERSION__"
- In reply to: Adrian Gibbons: "Getting the units of a number"
- Next in thread: Artie Gold: "Re: Getting the units of a number"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|