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

user1613360
  • 1,280
  • 3
  • 16
  • 42
  • 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? – thegrinner Aug 08 '14 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. – Jon Skeet Aug 08 '14 at 12:08
  • @JonSkeet I never thought of that thank you very much I have edited the question asap. – user1613360 Aug 08 '14 at 12:11
  • Right. That's *much* easier to understand - and the `setScale()` part of my answer is what you want. – Jon Skeet Aug 08 '14 at 12:11

2 Answers2

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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));
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • It will change the value I just want to add 1200 trailing zeros without moving the decimal point. – user1613360 Aug 08 '14 at 12:02
  • @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? – Jon Skeet Aug 08 '14 at 12:03
  • @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. – Jon Skeet Aug 08 '14 at 12:06
  • @user1613360 actually what are you going to do here? I got your question as you want to multiplying x by 10^1200 – Ruchira Gayan Ranaweera Aug 08 '14 at 12:06
  • @RuchiraGayanRanaweera I am calculating the value of pi and using intermediate results will improve the speed and accuracy. – user1613360 Aug 08 '14 at 12:08