0

My code is:

x=new BigDecimal(x.toString()+" "+(new BigDecimal(10).pow(1200)).toString());

I am trying to concat two bigdecimals after converting them to string and then convert them back to bigdecimal.I am doing so because x has a value with a decimal point and multiplying will change its value.It compiles fine but throws a numberformat exception on execution.

My idea was to use the BigDecimal("String x") constructor to convert the string back to BigDecimal.

Note: (for example) x=1333.212 and I am converting it to x=1333.2120000

4
  • So if I understand, you want to take a number like 123.123 and turn it into 1230...0.123, where there are 10^1200 zeros in that truncated portion? Commented Aug 8, 2014 at 12:08
  • 1
    That's still not what you want, because 10^1200 starts with a "1" which I suggest you don't want. It would really help if you'd give a simple example of input and expected output - with just (say) 4 digits in the input, and adding an extra 5. Commented Aug 8, 2014 at 12:08
  • @JonSkeet I never thought of that thank you very much I have edited the question asap. Commented Aug 8, 2014 at 12:11
  • Right. That's much easier to understand - and the setScale() part of my answer is what you want. Commented Aug 8, 2014 at 12:11

2 Answers 2

5

It's failing to parse because you've got a space in there for no particular reason. Look at the string it's trying to parse, and you'll see it's not a valid number.

If you're just trying to add extra trailing zeroes (your question is very unclear), you should change the scale instead:

x = x.setScale(x.scale() + 1200);

Simple example:

BigDecimal x = new BigDecimal("123.45");
x = x.setScale(x.scale() + 5);
System.out.println(x); // 123.4500000

If you were trying to multiply the value by a power of 10 (as your initial question suggested), there's a much simpler way of doing this, using movePointRight:

x = x.movePointRight(1200);

From the docs:

The BigDecimal returned by this call has value (this × 10n) and scale max(this.scale()-n, 0).

Sign up to request clarification or add additional context in comments.

2 Comments

I just want to add 1200 trailing zeros and I do not want to move the decimal point.
@user1613360: See my edit... your question is still badly worded though.
0

You need to try in this way

BigDecimal result=new
           BigDecimal("1333.212123232432543534324243253563453423423242452543535")
                                     .multiply(new BigDecimal("10").pow(1200));
System.out.println(result);

For your Edit:

BigDecimal result=new 
          BigDecimal("1333.212123232432543534324243253563453423423242452543535")
                                       .multiply(new BigDecimal("10").pow(1200));
System.out.println(result.movePointRight(-1200));

5 Comments

It will change the value I just want to add 1200 trailing zeros without moving the decimal point.
@user1613360: That's not what your question says. You've said: "I am converting it to x = 1333.212... * 10^1200". What do you think that "* 10^1200" would do?
@user1613360: Your question still isn't clear, because you're still talking about multiplying x by 10^1200, which you don't actually want to do.
@user1613360 actually what are you going to do here? I got your question as you want to multiplying x by 10^1200
@RuchiraGayanRanaweera I am calculating the value of pi and using intermediate results will improve the speed and accuracy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.