Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
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
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Paul Clapham
• Knute Snortum
• Rob Spoor
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Piet Souris
• Stephan van Hulst
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# Breaking a number into digits

Greenhorn
Posts: 7
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).

Rob.

Ranch Hand
Posts: 1249

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).

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
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.

Ranch Hand
Posts: 257
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

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

 It is sorta covered in the JavaRanch Style Guide.