• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Big Decimal to String

 
Marie
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to do something like below,

_xmlutil.node("InvoiceAmount", _xmlutil.escapeXML(BigDecimal.toString(i.getInvoiceAmount())))

But toString is not supported by BigDecimal.

How do I convert my BigDecimal value to a string and add as a node in my XML document ?

Thanks
Mei
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get a string representation of a BigDecimal object, just call the toString() method (inherited from Object).
If you need particular formatting, get the doubleValue() and format with java.text.DecimalFormat or use the various Formatter convenience methods if you're using 1.5.

Cheers.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mei Fdo:
...toString is not supported by BigDecimal...


:roll:
 
Marie
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for the replies,

It compiled ok,

How do I initialize a BigDecimal number ?

(like double dd = 0D, float ff = 0F)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mei Fdo:
... How do I initialize a BigDecimal number? ...

These are objects, so you instantiate by calling a constructor. For example...

BigDecimal big = new BigDecimal("987654321");

Note that once you have your BigDecimal object(s), you manipulate by calling methods -- not by using operators as you would with primitives.

BigDecimal bigOne = bigTwo.add(bigThree); //NOT bigTwo + bigThree

See the API for info...

http://java.sun.com/j2se/1.5.0/docs/api/index.html
 
Marie
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I called the constructor as per http://java.sun.com/j2se/1.3/docs/api/

Amount=new BigDecimal(0D);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mei Fdo:
Yes, I called the constructor as per http://java.sun.com/j2se/1.3/docs/api/

Amount=new BigDecimal(0D);

Did you note that "the results of this constructor can be somewhat unpredictable"? Compare the API's description of the BigDecimal constructor that takes a double vs. the BigDecimal constructor that takes a String.

(Also, did this work for you? I'm assuming that the variable "Amount" -- which should be lowercase by convention -- was previously declared as type BigDecimal.)
[ September 10, 2005: Message edited by: marc weber ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

These are objects, so you instantiate by calling a constructor. For example...

BigDecimal big = new BigDecimal("987654321");

Note that once you have your BigDecimal object(s), you manipulate by calling methods -- not by using operators as you would with primitives.

BigDecimal bigOne = bigTwo.add(bigThree); //NOT bigTwo + bigThree

See the API for info...

http://java.sun.com/j2se/1.5.0/docs/api/index.html


Umm...I think bigFive would be a clearer variable name. :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic