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
Tag: bigdecimal
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
How to multiply a BigDecimal by an integer in Java
How do you multiply a BigDecimal by an integer in Java? I tried this but its not correct. Answer You have a lot of type-mismatches in your code such as trying to put an int value where BigDecimal is required. The corrected version of your code:
Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part
I have a BigDecimal number and i consider only 2 decimal places of it so i truncate it using: Now I want to print it as String but removing the decimal part if it is 0, for example: 1.00 -> 1 1.50 -> 1.5 1.99 -> 1.99 I tried using a Formatter, formatter.format but i always get the 2 decimal
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 change the decimal separator of DecimalFormat from comma to dot/point?
I have this little crazy method that converts BigDecimal values into nice and readable Strings. It however, also produces a so called grouping separator “,” that makes all my values come out like this: I do need the separator to be a dot or a point and not a comma. Does anybody have a clue of how to accomplish this
Which PostgreSQL column type should be used to store a Java BigDecimal?
What PostgreSQL column type should I use to store a Java BigDecimal? Answer See PostgreSQL datatypes – perhaps Numeric, which can act as an arbitrary precision type (this is a PostgreSQL extension). …without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. I
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