• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to remove digits of number ?  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the java, how to remove digits of number in an efficient way ? for example, I have 13 digits number, I want to remove the first 3 digits, how to do it ? what is the best practice ?

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
538
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type is holding the number? String? int? double?

And if a String, is there anything other than digits in the string? (like letters, dashes, spaces, etc).

The answer of how to go about it depends on the answer to these questions so please do reply.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the % and / operators. Note those are links.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:What type is holding the number? String? int? double?

And if a String, is there anything other than digits in the string? (like letters, dashes, spaces, etc).

The answer of how to go about it depends on the answer to these questions so please do reply.


the type is Long
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example of how to drop the leading digits of a number:



Note that the % operator, which does a "modulo" calculation, is being used there. you should be able to generalize that example to other examples.

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about -12345 % 1000?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!