You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by NickDeGraeve <ni...@pandora.be> on 2010/01/15 15:10:42 UTC

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: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


RE: Decimal rounding issue

Posted by Wing Yew Poon <wi...@oracle.com>.
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:nick.degraeve@pandora.be] 
Sent: Friday, January 15, 2010 6:11 AM
To: user@xmlbeans.apache.org
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: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: Decimal rounding issue

Posted by cmoller <ch...@gmail.com>.
I can tell you how I do this, but I hope someone has a better solution! I
currently set the scale on BigDecimal (setScale), which will force its
output to be legal. 

If there is a better solution, PLEASE let Nick (and me) know!


NickDeGraeve wrote:
> 
> 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-tp27176674p27457488.html
Sent from the Xml Beans - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org