Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal issue

 
Hd Desai
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the difference between BigDecimal bd = new BigDecimal (1) and BigDecimal bd = new BigDecimal("1") ? We are using java 1.4

Please let me know about the same.

Thank you in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hd Desai wrote:Hi,

What is the difference between BigDecimal bd = new BigDecimal (1) and BigDecimal bd = new BigDecimal("1")


What do the docs tell you?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably won’t find much difference. But try. . . and (I hope) there will be a difference.
 
Hd Desai
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for response Jeff.

I went through the docs before posting here..Was not able to get clear answer. BigDecimal = new BigDecimal("1") creates new object with 1 as string...while BigDecimal = new BigDecimal(1) is as BigInteger...but in any case it should work. But this program is failing.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! I have, more by luck than good management, hit on the same values they quote in the API documentation!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using whole numbers throughout?
 
Hd Desai
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Ritchie.

no it is not whole number. it is number with 6 scale ..

 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try my little example? Did you read the constructor documentation? I would have thought those would be very helpful.

And always tell us what is going wrong.
 
Hd Desai
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again Ritchie...yes I read whole doc..and it was my mistake i was reading 1.5 javadoc...

BigDecimal constructor does not take int till 1.5 . Now got it all.

Thank you very much once again...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hd Desai wrote:But this program is failing.

just as a tip for next time...just saying "my program doesn't work" is really pointless. There are thousands of ways it could "not work". One of three things should be happening:

1) You get a compiler error. If so, tell us the EXACT text of that error. There is a LOT of info there, once you learn how to read it
2) Your program crashes when it runs. Again, you'll get some kind of message telling you what went wrong.
3) Your program runs, but behaves differently than you expect. We don't know what you expect, nor do we know what it does do. You need to tell us both.

You will find you generally get much more help, better help, and quicker help, if you do the above.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hd Desai wrote:Thank you for response Jeff.

I went through the docs before posting here..Was not able to get clear answer. BigDecimal = new BigDecimal("1") creates new object with 1 as string...while BigDecimal = new BigDecimal(1) is as BigInteger


It says a lot more than that. You probably ought to read it, very closely. And a little bit of thinking and effort would have led you to try something like this:




...but in any case it should work. But this program is failing.


Saying it "should work" is meaningless. It's doing exactly what it should do. The problem is that you have erroneous assumptions about how a given bit of code "should" behave.

And note that ItDoesntWorkIsUseless.(⇐click) You need to TellTheDetails(⇐click) so that people here will understand exactly what is going wrong.
 
Hd Desai
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred for point int out and I completely agree with you on this. Will take care next time.

Thank you for your time and effort.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hd Desai wrote:thanks again Ritchie...yes I read whole doc..and it was my mistake i was reading 1.5 javadoc...

BigDecimal constructor does not take int till 1.5 . . . .
What has an int got to do with it? You said earlier you were not using whole numbers throughout. So some of your code uses the BigDecimal(int) constructor and the rest the BigDecimal(double) constructor? As Jeff has said, we need to know the correct details before we can help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic