RE: Decimal rounding issue

Wing Yew Poon <[email protected]>
Newsgroups gmane.text.xml.xmlbeans.user
Message-ID <4b322040-9d1d-4f10-8258-7e138d5d37ef@default>
Nick,
this is a pure Java question, nothing to do with XMLBeans really.
You are using the BigDecimal(double) constructor; you probably
want to use the BigDecimal(String) constructor.
The phrase to keep in mind is: decimal representation of binary
floating point value.
See http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double).
- Wing Yew

-----Original Message-----
From: NickDeGraeve [mailto:[email protected]] 
Sent: Friday, January 15, 2010 6:11 AM
To: [email protected]
Subject: Decimal rounding issue


I have an XSD from which I generated my XmlBeans classes.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:me="http://my.url" 
	targetNamespace="http://my.url" >
	<xs:element name="MyElement">
		<xs:complexType>			
			<xs:attribute name="myAttribute" type="me:ValueType" />
		</xs:complexType>
	</xs:element>
	<xs:simpleType name="ValueType">
		<xs:restriction base="xs:decimal" >
			<xs:fractionDigits value="2" />
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

Wheh I run this JUnit test case:

public class XmlBeansTestCase extends TestCase {

   public void test() {
      MyElementDocument document = MyElementDocument.Factory.newInstance();
      MyElement myElement = document.addNewMyElement();
      myElement.setMyAttribute(new BigDecimal(12.65));
      System.out.println(document);
      XmlOptions options = new XmlOptions();
      Collection errors = new LinkedList();
      options.setErrorListener(errors);
      boolean valid = document.validate(options);
      System.out.println(errors);
      assertTrue(valid);
   }
}

the test fails and I get this output:

<MyElement
my:myAttribute="12.6500000000000003552713678800500929355621337890625"
xmlns="http://my.url" xmlns:my="http://my.url"/>
[error: cvc-fractionDigits-valid: Decimal fractional digits (49) of value
'12.6500000000000003552713678800500929355621337890625' does not match
fractionDigits facet (2) for ValueType in namespace http://my.url]

Why does it come out like that and not as "12.65"? 
How can I get it right?
-- 
View this message in context: http://old.nabble.com/Decimal-rounding-issue-tp27176674p27176674.html
Sent from the Xml Beans - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.