473,385 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting the units of a number

Hello,

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?

Regards,

Adrian.
Jul 19 '05 #1
4 2409
> 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
Jul 19 '05 #2
Adrian Gibbons wrote:
Hello,

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?

Erm, why are you working so hard? ;-(

int get_units( int n ) {
return n % 10;
}

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #3
"Adrian Gibbons" <ad***********@yahoo.co.uk> wrote in message
news:34**************************@posting.google.c om...
Hello,

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?


You do know that any integer mod 10 is the units part (12345 % 10 == 5)
don't you? Or did you mean the number of digits in the integer?
Jul 19 '05 #4
Peter van Merkerk wrote:
I need to get the units of a number.

i.e. if I had 12345 then I would want '5'.

This is the number of digits, rather than the numeral in the "ones" place...

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


You want to initialize units to 1 or use a do/while loop. Otherwise, the
number 0 has 0 units...

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nick Goloborodko | last post by:
Hi, I've got the following problem: i need to build a "progress bar" with JavaScript. So far i have the following configuration: i've got a DIV inside of a DIV. Each DIV has a style attribute...
5
by: Javaman59 | last post by:
I just saw an interesting identifier in a C# book I'm reading... double dollarsPerHead This jumped out at me, as it is not common to put units in an identifer. We don't see...
38
by: axlq | last post by:
I'm trying to figure out how to display a box that has a width in "em" units. So far no luck. Below is some HTML that displays two rows of 30 'm' characters in lowercase and uppercase, followed...
2
by: TheSeeker | last post by:
Hi, As part of a larger project, I am trying to use the GNU Units program to provide unit conversions between quantities. My first iteration, which worked OK, was to simply use units as a...
4
by: beatdream | last post by:
I am designing a database to handle different kinds of products ... and these products can have different properties...for example, a trouser can be specified by the width, length, color, and other...
18
by: kwikius | last post by:
Well lads :-)... looking at your Boost Units library the impression I get is that you got your library into boost Before writing anything much apart from some documentation, which was AFAICS even...
2
by: pukhton | last post by:
I am getting this error each time I run my form in Access. I have a table calls "Medications" and it has three colums (Medication_Name, Concentration, Units) then I have another table name Main...
1
by: jonnyothan | last post by:
The following code isn't producing expected results: class Selectable { bool IsSelected() const; }; class Unit : public Selectable { };
2
by: andersond | last post by:
I have a webpage that allows insurance agents to enter a group of trucks for an insurance policy. I want to keep track of the number of power units (tractors or trucks), the number of trailers and...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.