• Post Reply Bookmark Topic Watch Topic
  • New Topic

number ending with 1  RSS feed

 
Botond Lakatos
Greenhorn
Posts: 9
Chrome Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Assuming you are dealing with numbers greater than 9, you won't just have to worry about the last digit. . . .
Really? What about 21st?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. . . .
Really? What about 21st?



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
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James is talking about 11st and 12nd
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are correct. I misread it. Sorry.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.Calendar for that.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I didn't connect the two threads together before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!