• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaring temporary BigDecimals in shorthand  RSS feed

 
Greenhorn
Posts: 6
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I'm using a sledgehammer to smash a gnat. Working on a little program to convert Celsius to Fahrenheit and decided to use BigDecimals so I can get better acquainted with how Java uses math on BigDecimals. Just to be clear, I know a float would do the job but I want to use BigDecimal for the practice. Anyway, I noticed after some bugtesting that I can't drop regular numbers into a BigDecimal math equation, I have to declare each number in the equation a BigDecimal. This is kind of bulky, even though it makes perfect sense, and I was wondering if there was shorthand for the declarations.

Here's a chunk of my code. This compiles just fine and calculates the temperature acceptably, but I'd like to avoid, for instance, having to declare 'new BigDecimal("9.0")' if supported by the language.



Thanks!
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the valueOf static methods.
Although this will probably create new BigDecimal objects in the background, it may also make use of internal caches for common values.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Adrian. The only shortcut I found was using valueOf which is essentially converting something like double to BigDecimal -- seems kind of pointless in a way. I was going to use BigDecimal for this project but realized it was much more of a hassle than using double. BigDecimal works great when dealing with currency, but with temperatures it was just too many extra lines of code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!