This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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
Posts: 39054
714
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: 62164
193
  • 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: 23963
50
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: 62164
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about -12345 % 1000?
 
I'm full of tinier men! And a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!