• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to remove digits of number ?  RSS feed

 
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
 
author & internet detective
Sheriff
Posts: 37820
582
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.
 
Marshal
Posts: 57260
175
  • 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
 
Sheriff
Posts: 23116
45
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: 57260
175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about -12345 % 1000?
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!