Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Breaking a number into digits  RSS feed

 
Rob Lord
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am relatively new to Java and J2EE, and have programmed a banking web application at work.

However, there is an algorithm I need to program and it involves breaking a number down to do calculations on the individual numbers that make it up.

The number will be about 14 digits long max, but I was wondering what the best way to break the number down in java is (ie the number 123456789 will need to have the last two digits added to the fifth digit etc).

Any advice will be appreciated.

Rob.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Lord:
Hello

I am relatively new to Java and J2EE, and have programmed a banking web application at work.

However, there is an algorithm I need to program and it involves breaking a number down to do calculations on the individual numbers that make it up.

The number will be about 14 digits long max, but I was wondering what the best way to break the number down in java is (ie the number 123456789 will need to have the last two digits added to the fifth digit etc).

Any advice will be appreciated.

Rob.


Dear Rob,

What are you looking for I can illustrate you for an example:

if you have any digit e.g 1245364
then you need every digit seperately like
4
6
3
5
4
2
1

This is an example. If you would like to this one only... Then it quite easy.

I can show you a code snippet



This would create following output according to the input "123234"

a[0]=4
a[1]=3
a[2]=2
a[3]=3
a[4]=2
a[5]=1


Hope your problem has been covered in this logic...

If still any concern then revert with concerns.
 
Rob Lord
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response Ankur, that's what I was after.

I know it probably seems simple to most, but I couldn't figure out a simple way to do it, and couldn't seem to find an example anywhere.

I will print this post off for future reference and your answer is much appreciated Ankur.

Rob.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The % and / operator will help a lot. If you have worked with C or C++, then you might have seen lot of tricky programs with these two operators.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivas Kalvala:
Hello,

The % and / operator will help a lot. If you have worked with C or C++, then you might have seen lot of tricky programs with these two operators.


Did You just bumped the thread???..

Anyways thankx for this usefull Important Information
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!