| Author |
Big Decimal to String
|
Marie
Greenhorn
Joined: May 04, 2005
Posts: 28
|
|
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
|
Thanks<br />Marie
|
 |
Steve Morrow
Ranch Hand
Joined: May 22, 2003
Posts: 657
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Mei Fdo: ...toString is not supported by BigDecimal...
:roll:
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Marie
Greenhorn
Joined: May 04, 2005
Posts: 28
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
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
Joined: May 04, 2005
Posts: 28
|
|
Yes, I called the constructor as per http://java.sun.com/j2se/1.3/docs/api/ Amount=new BigDecimal(0D);
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
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
Joined: Dec 06, 2001
Posts: 3061
|
|
Umm...I think bigFive would be a clearer variable name. :roll:
|
Java API Documentation
The Java Tutorial
|
 |
 |
|
|
subject: Big Decimal to String
|
|
|