Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Break down BigDecimal to individual values

1. ## Break down BigDecimal to individual values

Assuming that I have a BigDecimal. What would be the best apprach to breaking that down into indivual digits so I can utilise the individual values. I know there are several ways this can be done but I was looking for the most effective way of acheiving this and was wondering if there is a method that can deal with this specific case. I've looked at the API but I can't see anything that is immedietly obviouse.

NB. Pehaps BigDecimal isn't the best way to handle this!

2. ## Re: Break down BigDecimal to individual values

Sorry: I need to work with the individual values as int so if possible I'm trying to avoid going from BigDecimal > String (or char[] etc.) > int.

3. ## Re: Break down BigDecimal to individual values

I really didn't get you. Can you kindly be more descriptive?

4. ## Re: Break down BigDecimal to individual values

Sorry...

Ok, I have a number that is 1000 digits long. its too big to be of type long so I have had to make it a BigDecimal. Lets say for example that the number starts:

9837425678934653......

I'm trying to find out if there is a way of breaking that number down so that I can work with those individual numbers.

for example 9+8+3+7+4+2+5+6+7+8+9+3+4+6+5+3......

Now I can actually do this several ways; for example by converting the original number into a String. I can then work with the charAt() or I can create a Vector or array and fill it with the individual digits... BUT because I need to work with those individual digits as numbers that requires me to change the individual digits back from a char to an int. I was wondering if there was a better way of doing this without having to make the 'extra' change of type. ie. can I directly isolate that the 4th digit in this BigDecimal is 7?

5. ## Re: Break down BigDecimal to individual values

That's probably the easiest method for isolating decimal digits in a BigDecimal.

What overall problem are you trying to solve? Perhaps there's a better way to solve that.

6. ## Re: Break down BigDecimal to individual values

I'm working on two seperate problems both using BigDecimal. One of them is simply to calculate the product of the digits that make up 2 to the power of 1000. I've successfully done this but I was just curiouse if there was a better way of doing it.

I have however run into a problem:

I need to run a for loop as follows:
```for (int i = someValue; i > 0; i--) {
product *= i;
}```
The problem is that 'product' needs to be a BigDecimal as its too large for a long. My attempts thus far have been to make i a BD, then I have setup a new BD with a value of 0 and used the compareTo() to replace the i > 0; I then needed to setup ANOTHER BD with a value of 1 and use the subtract() to replace the i--. I have now come a bit unstuck with the sum *= i part and how to make that work with BD.

I tried setting up yet another BD called sum and then doing this:

`sum = bd1.multiply(bd1.multiply(i));`

It all seems like alot of BD and not only that but it doesn't actually work!

7. ## Re: Break down BigDecimal to individual values

Using BigDecimals and BigIntegers in Java has always been a pain.

You're last bit of code is actually computing sum = sum * sum * i, not sum = sum * i

8. ## The Following User Says Thank You to helloworld922 For This Useful Post:

tarkal (November 13th, 2011)

9. ## Re: Break down BigDecimal to individual values

Ok so I'm basically olong the right tracks, I havn't missed any easy solution. Thanks very much.

10. ## Re: Break down BigDecimal to individual values

On the topic of BigDecimal. How would you go about computing a number that exceeds the memory limit of the computer?

I'm trying to calculate 100! (ie. 100 x 99 x 98 x 97 .... 3 x 2 x 1), but this is beyond the capability of the current virtual enviroment. Obviousley the short answer is that I could just boost the RAM in my enviroment but it did pose an interesting question and I wanted to see if I could investigate solving the problem from a programing perspective rather than a hardware one.

11. ## Re: Break down BigDecimal to individual values

I don't see why you would be running out of memory, 100! should fit in ~66 bytes, and even in it's string representation is ~158 characters long.

The JVM's default heap size is quite "small" compared to the memory sizes of modern computer (I think it's ~64 MB, don't quote me on this). The first thing to try is to increase the max heap size (google for "increase java heap size"), the second option is simply to get more memory. There's no way around requiring more memory. Modern OS's have a way to "cheat" the system by using virtual memory, or using hard disk space as memory, but it's extremely slow.

12. ## Re: Break down BigDecimal to individual values

Thanks I'll try that next.

First its getting late and I should probably call it a day but I'm still fighting this BigDecimal issue... I can't use long for the 100! so I have had tio move to BigDecimal so this is my attempt to convert the for loop into a BigDecimal loop. But all this code is pumping out is '1'.
```int start = 10;
BigDecimal zero = new BigDecimal(0);
BigDecimal one = new BigDecimal(1);
BigDecimal bang = new BigDecimal(1);

for (BigDecimal i = new BigDecimal(start); i.compareTo(zero)== 0; i.subtract(one)) {
bang = bang.multiply(i);
}

System.out.println(bang);```

???

13. ## Re: Break down BigDecimal to individual values

I would recommend against using BigDecimal for this kind of operation, us a BigInteger instead.

Second, you're update loop never re-assigns i. BigDecimals and BigIntegers are immutable and need to be re-assigned a value.

Lastly, you're loop condition is if i == 0. A for loop runs while the condition is true, not false. This condition is initially false, so the loop never gets executed.

14. ## The Following User Says Thank You to helloworld922 For This Useful Post:

tarkal (November 14th, 2011)

15. ## Re: Break down BigDecimal to individual values

How would I go about re-assigning a value to an immutable object? Would I have to create a new BigInteger for every time the loop is excecuted?

16. ## Re: Break down BigDecimal to individual values

Nope, just re-assign it to the same variable. The operation will return a new BigInteger already.

```BigInteger a = new BigInteger("1");

17. ## The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

Mr.777 (November 15th, 2011), tarkal (November 15th, 2011)

18. ## Re: Break down BigDecimal to individual values

In your case it would be
`i=i.subtract(one)`

Well, i wanted to subscribe to this post, that's why i posted here. I learnt some new things by helloWorld922. I am not sure for which post i should say thanks to you

19. ## Re: Break down BigDecimal to individual values

Thanks for that. I went off on a bit of a tangent and after extensive experimenting (and before checking back here to find your answer) i tried several alternative solutions. Turns out I can't create a new BigInteger for each iteration anyway as it seems that it is not possible to create variables at run time. I was trying to find a way of renaming the variables dynaically as they were created but it wasn't having any of it.

Its beyond the scope of the innitial discussion but seeing as we have come this far... does anybody know why you can't create variables at run time in Java? As in the literal restriction placed by the JVM.

20. ## Re: Break down BigDecimal to individual values

Its beyond the scope of the innitial discussion but seeing as we have come this far... does anybody know why you can't create variables at run time in Java? As in the literal restriction placed by the JVM.
Becuase.... a variable has associated type and also called comile-time type. A variable must always contain a value that is compatible to it's assignment with it's type. Also, creating a variable at run time is almost impossible in java coz variable names exist only, until the code is compiled and also .class file doesn't have space for variables used in the specific .java file.
First, why do you need to declare a variable at run time?
Second, you will have to write very own code to handle that created variable in a very specific manner.
But you can solve this problem with many different ways, like using Collections framework (as Vector is not recommended anymore, ArrayList, Map(Recommended in this scenario))Thanks

21. ## Re: Break down BigDecimal to individual values

Thanks Mr.777, I'm affraid I didn't really understand you explanation! I actually have no need or intention to create variables at run time. As you say there are plenty of alternative solutions and I just happened to stumble accross the problem by trial and error and I was really asking the question to try and extend my understanding of the JVM.