How can I convert a BigDecimal to a 2-decimal-place string?

bigdecimal round to 2 decimal places
bigdecimal format to 2 decimal places
format double to 2 decimal places java
ruby bigdecimal
ruby display 2 decimal places
java truncate double to 2 decimal places
math.round java 2 decimal place
string.format to remove decimal places in java

I want to convert a BigDecimal object to a currency value to two decimal places. I don't want any rounding. How can I do it?

None of the following approaches worked:

v = BigDecimal("7.1762")
w = BigDecimal("4.2")

v.to_s('2F')            # => "7.17 62"
v.to_s('F')             # => "7.1762"
v.to_s('%0.2F')         # => "0.71762E1"
v.to_s('%0.2f')         # => "0.71762E1"
v.truncate(2).to_s('F') # => "7.17" # This one looks like it worked
w.truncate(2).to_s('F') # => "4.2"  # But it doesn't pad with the trailing zero(es)

How about combining BigDecimal#truncate and String#%? :

"%.2f" % BigDecimal("7.1762").truncate(2)
# => "7.17"
"%.2f" % BigDecimal("4.2").truncate(2)
# => "4.20"

BigDecimal toString() Method in Java with Examples, in base ten using the characters '0' through '9' with no leading zeros except when the value is 0, then single character '0' is used. A standard canonical string form of the BigDecimal is created by converting the absolute value of the unscaled value of BigDecimal in base ten using the characters ‘0’ through ‘9’ with no leading zeros except when the value is 0, then single character ‘0’ is used.

The comments on the accepted answer contain important information.

It's been rightly pointed out that in older versions of Ruby, using "%.2f" % v will result in v being implicitly converted to a floating-point number, causing potential loss of precision/accuracy - e.g. "%.2f" % (10**24) == "999999999999999983222784.00"

You can convert BigDecimal to String accurately, even in older Rubies, by using .to_s("F"). This returns the number in fixed-point format, with a decimal point followed by zero or more decimal digits.

If you add "00" onto the end of the number (to ensure it has two or more decimal digits), then you can then strip it down using a regex like /.*\..{2}/ (any characters; a decimal point; then two more characters), so it only keeps the first two digits after the decimal point.

require 'bigdecimal'
w = BigDecimal("4.2")

 w.truncate(2).to_s("F")                       # => "4.2"
(w.truncate(2).to_s("F") + "00")               # => "4.200"
(w.truncate(2).to_s("F") + "00")[ /.*\..{2}/ ] # => "4.20"

Difference between float and double in C/C++, How do I convert a double to a string in Java? Previous Next In this post, we will see how to convert BigDecimal to String in java. There are many ways to convert BigDecimal to String in java. Some of them are: Using toString() Using String.valueOf() toString method You can simply use BigDecimal’s toString method to convert BigDecimal to String. When you run above program, you will get below output: 20.234 String’s ValueOf method You

Simply formatting with '%.2f' should work

v = BigDecimal("7.1233")
"%.2f" % v #=> "7.12"

v = BigDecimal("7.1")
"%.2f" % v #=> "7.10"

Convert Double to String, Removing Decimal Places, Convert Double to String, Removing Decimal Places 2. Truncation Using Casting. If our double value is within the int range, we can We can use BigDecimal's setScale method to tell whether we want to round or truncate:. The BigDecimal can not be a double. you can use Int number. if you want to display exactly own number, you can use the String constructor of BigDecimal . like this: BigDecimal bd1 = new BigDecimal("10.0001"); now, you can display bd1 as 10.0001. So simple. GOOD LUCK.

I think this should work, although a little verbose:

money_amount = BigDecimal.new(100)
money_amount.truncate(2).to_s('F').ljust(money_amount.truncate(0).to_s('F').length + 1, '0')

How to Round a Number to N Decimal Places in Java, Learn different ways of getting the number of digits in an Integer in Java. 2. Decimal Numbers in Java. Java provides two primitive types that can be used constructing BigDecimal; we must always use BigDecimal(String) constructor. Note that we can change rounding behavior by passing the desired  In this post, we will see how to convert String to BigDecimal in java. It is very simple to convert String to BigDecimal in java. You can simply use BigDecimal ‘s String based constructor to do it. When you run above program, you will get below output: Converted String currency to bigDecimalCurrency: 13.23.

Java – Display double in 2 decimal places – Mkyong.com, In Java, there are few ways to display double in 2 decimal places. public class BigDecimalExample { public static void main(String[] args)  The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal ("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one. When a double must be used as a source for a BigDecimal, note that this

Setting the Decimal Place of a Big Decimal Value : BigDecimal , Setting the Decimal Place of a Big Decimal Value : BigDecimal « Data Type « Java. BigDecimal; public class Main { public static void main(String[] argv) throws Exception { int decimalPlaces = 2; BigDecimal Convert Object to BigDecimal. In Java, the BigDecimal class has several methods that let you convert BigDecimal values to strings, doubles, or integers. Take a look at the following for a list of the conversion methods. Method Explanation double doubleValue() Returns the value of this BigDecimal as a double. int intValue() Returns the value of this BigDecimal as an …

java round double/float to 2 decimal places, You can convert double or float to BigDecimal and use setScale() method to round double/float to 2 decimal places. Here is the example: Java public static void main(String[] args) {. double d  Hope you understand the creation of a bigdecimal object in different ways. How to convert BigDecimal to String with an example in java? There are many ways we can convert to String in Java. Using toString() method. Every class which extends Object class has toString() method. this method returns the string form the number

Comments
  • "%.2f" % BigDecimal("40000000000000000.199999").truncate(2) - not sure if relevant to OP, but it is a problem because we are going via Float. It does somewhat defeat purpose of using BigDecimal in the first place though. It is solvable
  • @NeilSlater, How about this? (BigDecimal("40000000000000000.2").truncate(2).to_s('F') + '00')[/.*\..{2}/]
  • Yes I think that is safe because BigDecimal#to_s with 'F' will always add the decimal point and at least one digit.
  • "%.2f" % BigDecimal("100000000000000000000000000").truncate(2) => "100000000000000004764729344.00"
  • @falsetru my point is that with this answer there's also a possibility the numbers will changes before the decimals ...4764729344
  • it rounds the number: "%.2f" % BigDecimal("7.129") #=> "7.13"