• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to trim the zeros off of a BigDecimal?  RSS feed

 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a simple line of code I can use to make it so if the user types "2.00000000" the BigDecimal is set to "2"?


This is all I have so far and it only works on something like this
2.0000 = 2
but
2.1000 = 2.1000
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the BigDecimal documentation? Look for the words precision and scale, and also the setScale method. I think that is what you want.
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found ROUND.UP, but its only for ints

so I kept looking and found toPlainString()
it works fine


x is 2.00052

 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not remove the zeroes; it simply displays the result minus the zeroes. Have a look through this thread and the other thread quoted therein for different rounding options.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BigDecimal.stripTrailingZeros()
Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was getting really confused, thank you for the help!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!