programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# number ending with 1

Botond Lakatos
Greenhorn
Posts: 9
Hello
I need to add st, nd, rd, th endings to numbers (int) How do you check the last digit of the number to do this?
Thanks for taking the trouble and answering this

Henry Wong
author
Sheriff
Posts: 23295
125
Botond Lakatos wrote:Hello
I need to add st, nd, rd, th endings to numbers (int) How do you check the last digit of the number to do this?
Thanks for taking the trouble and answering this

One option is to use Java's remainder operator (aka modulus operator) to extract out the units digit.

Henry

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
How would YOU do it? I don't mean using java, but English. if you had to explain to someone who had never heard of these various pieces you mention, how would you do it?

THAT is how you start writing a program. You spend a major portion of your time THINKING about how to do it before you write any code.

James Boswell
Bartender
Posts: 1051
5
• 1
Assuming you are dealing with numbers greater than 9, you won't just have to worry about the last digit. Think about those where the second to last digit is 1.

Campbell Ritchie
Marshal
Posts: 56570
172
James Boswell wrote:Assuming you are dealing with numbers greater than 9, you won't just have to worry about the last digit. . . .

Henry Wong
author
Sheriff
Posts: 23295
125
Campbell Ritchie wrote:
James Boswell wrote:Assuming you are dealing with numbers greater than 9, you won't just have to worry about the last digit. . . .

I think you mis-read what James said. He was referring to the point that checking only the units digit is not enough (I bold the subtle point above).

Henry

Rob Spoor
Sheriff
Posts: 21135
87
James is talking about 11st and 12nd

Campbell Ritchie
Marshal
Posts: 56570
172
Yes, you are correct. I misread it. Sorry.

James Boswell
Bartender
Posts: 1051
5
All

Just to clarify what I meant.

1 => st
2 => nd
3 => rd
4...9 => th

These rules regarding the final digit and the suffix added to it are different when the second to last digit is 1 - in which case, the suffix is always th.

Botond Lakatos
Greenhorn
Posts: 9
Thanks, modulo operator nice I was already thinking about some coversion to strings or something. The exercise speciefies that 11th iends with the and stuff forgot to mention
Thanks for the input! Still no idea how to count the number of weeks though if the week starts on Monday

Rob Spoor
Sheriff
Posts: 21135
87
Check out java.util.Calendar for that.

Joanne Neal
Rancher
Posts: 3742
16
• 1
Rob Spoor wrote:Check out java.util.Calendar for that.

That's already been suggested here, so lets not start a separate conversation about it.

Rob Spoor
Sheriff
Posts: 21135
87
I agree. I didn't connect the two threads together before.