Skip to content
Advertisement

Tag: bigdecimal

BigDecimal, precision and scale

I’m using BigDecimal for my numbers in my application, for example, with JPA. I did a bit of researching about the terms ‘precision’ and ‘scale’ but I don’t understand what are they exactly. Can anyone explain me the meaning of ‘precision’ and ‘scale’ for a BigDecimal value? Thanks! Answer A BigDecimal is defined by two values: an arbitrary precision integer

Convert Java Number to BigDecimal : best way

I am looking for the best way to convert a Number to a BigDecimal. Is this good enough? Can we lose precision with the toString() method ? Answer This is fine, remember that using the constructor of BigDecimal to declare a value can be dangerous when it’s not of type String. Consider the below… This will not print 0.35, it

BigDecimal – to use new or valueOf

I came across two ways of getting BigDecimal object out of a double d. new BigDecimal(d) BigDecimal.valueOf(d) Which would be a better approach? Would valueOf create a new object? In general (not just BigDecimal), what is recommended – new or valueOf? Answer Those are two separate questions: “What should I use for BigDecimal?” and “What do I do in general?”

How to print formatted BigDecimal values?

I have a BigDecimal field amount which represents money, and I need to print its value in the browser in a format like $123.00, $15.50, $0.33. How can I do that? (The only simple solution which I see myself is getting floatValue from BigDecimal and then using NumberFormat to make two-digit precision for the fraction part). Answer It will use

Advertisement