Subjects
Home
Xalan extension functions
Fomatting question serializing DOM with pretty print
xalan with pull parser
Cannot find the declaration
Apache Xalan drop support to run on JRE 1 1 x
Why does Doctype change processing of a document
Node set to XML string via Java extensions in Xalan J: possible?
Templates/Transformers + thread safety???
Problem evaluating xpath with muliple prefix with different namespace
remove an arbitrary attribute from xsl output
Xalan3 XSLT 2 0 XPath 2 0 support?
Problem using compiled translets with Xalan !!
Xalan and jstl 1 1 problem with transform tag
NullPointer in DOM2DTM getLocalName
URIResolvers base parameter with xsltc and cascaded imports
Performance problem for Xalan J on intel dual core
Standard libraries in JAXP?
Serializing a DOM tree to XML file, customize entities replacement
Library Conflict Involving BCEL Library
A question on how users are using <xsl:message >
Kevin Cormier as a new Apache Xalan J committer
Struggling to iterate over tokenized string
Xalan count() trouble
Problem with recursive xpath
Error when switching to java 1 5
document( ' ')
Problem with Xalan2 7 0 transformation
cr/lf options
entity encoded XML
can xalan transform 2 xml using one xslt?
Xalan J JIRA defect review Monday October 16, 2006 from 2:00 to 3:30 pm ED
xsl transform with cdata section elements
xslt parameters not expanded
Weird behavior of XPath evaluate()
How to avoid <xsl:message > instruction prints stylesheet file informations ?
Cannot find SimpleTransform subdirectory after installing Xalan J
recover from document not found exceptions
jdk1 5 and Xalan jar differences?
Performance Issue
Error/Bug adding floating point numbers
XPathAPI: eval exp using nodes with default namespace
modifying xalan to output invalid XML
NullPointerException
mege two separate xml nodes into one
Is this a XALAN document identification bug?
is StylesheetRoot really java io Serializable ?
transform() fails for DOMSource but succeeds for StreamSource
Thoughts on Transformer parameter passing
HELP, Xalan and jstl 1 1 problem with transformer
Problem with XPath namespace axis?
string utils:replace deleting search string if replacement string is an HTML
help with enumeration values pls
xalan 2 5 1 vs 2 7 performance question
How to insert/update in XML document
HTML Serialization and Handling of Ampersands in HREF Attributes
XHTML link tag stripping
SystemId Unknown; Line #24; Column #49; java lang NullPointerException
xpath text() help
Apostrophe problem with xalan 2 7 0
How to set variables in XML document?
Links
Home
Oracle database error code ...
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
Error/Bug adding floating point numbers

Error/Bug adding floating point numbers

2006-08-31       - By Timothy Jones
Reply:     1     2     3  

I feel your pain, Ambika, really I do...



This is the reason my output stylesheets are riddled with
format-number().



       <xsl:value-of select="format-number(number(@(protected)),
'#,##0.00')" />

and

       <xsl:variable name='roundedValue'
select='format-number(number($rawValue), "0.00")' />



But in my case, I know I am working with dollars/cents, so I know what
my output precision should be.  I fully appreciate that in other cases,
it may not be so clearly defined.



The same kind of behavior exists in languages that have 'float' and
'double' types (C, C++, Java, etc, probably Pascal's "real" type too).
To combat this for currency values, I convert them to the biggest
integer type and process them as cents: 32-bit long in "old C", 64-bit
long in Java, and "long long" on modern C/C++ compilers.  At least then
you are in complete control of your math.  Java also has
java.math.BigDecimal which doesn't do the IEEE-754 behavior either, but
it has the overhead of being a java.lang.Object subclass, whereas the
primitive integers/longs work faster.  



I have never understood how people can accept this rounding over the
years, but these are my workarounds.





Timothy  Jones

Syniverse Technologies

Work

(813) 637-5366

Sr. Systems Engineer  

Cell

(813) 857-7650

Development, Tampa, FL



<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas
-microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office
:smarttags" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="State"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="place"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="City"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
/* Font Definitions */
@(protected)
  {font-family:"Bradley Hand ITC";
  panose-1:3 7 4 2 5 3 2 3 2 3;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
  {margin:0in;
  margin-bottom:.0001pt;
  font-size:12.0pt;
  font-family:"Times New Roman";}
a:link, span.MsoHyperlink
  {color:blue;
  text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
  {color:blue;
  text-decoration:underline;}
p
  {mso-margin-top-alt:auto;
  margin-right:0in;
  mso-margin-bottom-alt:auto;
  margin-left:0in;
  font-size:12.0pt;
  font-family:"Times New Roman";}
span.EmailStyle18
  {mso-style-type:personal-reply;
  font-family:Arial;
  color:navy;}
@(protected) Section1
  {size:8.5in 11.0in;
  margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
  {page:Section1;}
-->
</style>

</head>

<body lang=EN-US link=blue vlink=blue>

<div class=Section1>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>I feel your pain, Ambika, really I do...<o
:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>This is the reason my output stylesheets
are riddled with format-number().<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp
;&lt;xsl:value-of
select=&quot;format-number(number(@(protected)), '#,##0.00')&quot; /&gt;<o:p><
/o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>and<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp
;&lt;xsl:variable
name=&#8217;roundedValue&#8217; select=&#8217;format-number(number($rawValue),
&#8220;0.00&#8221;)&#8217;
/&gt;<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>But in my case, I know I am working with
dollars/cents, so I know what my output precision should be.&nbsp; I fully
appreciate that in other cases, it may not be so clearly defined.<o:p></o:p><
/span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>The same kind of behavior exists in
languages that have &#8216;float&#8217; and &#8216;double&#8217; types (C, C++,
Java, etc, probably Pascal&#8217;s &#8220;real&#8221; type too). &nbsp;To
combat this for currency values, I convert them to the biggest integer type and
process them as cents: 32-bit long in &#8220;old C&#8221;, 64-bit long in Java,
and &#8220;long long&#8221; on modern C/C++ compilers.&nbsp; At least then you
are in complete control of your math. &nbsp;Java also has java.math.BigDecimal
which doesn&#8217;t do the IEEE-754 behavior either, but it has the overhead of
being a java.lang.Object subclass, whereas the primitive integers/longs work
faster.&nbsp; <o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>I have never understood how people can
accept this rounding over the years, but these are my workarounds.<o:p></o:p><
/span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<div>

<table class=MsoTableGrid border=0 cellspacing=0 cellpadding=0
style='border-collapse:collapse'>
<tr>
 <td width=180 colspan=2 valign=top style='width:134.85pt;padding:0in 5.4pt
0in 5.4pt'>
 <p class=MsoNormal align=center style='text-align:center'><strong><b><i><font
 size=3 color=teal face="Bradley Hand ITC"><span style='font-size:12.0pt;
 font-family:"Bradley Hand ITC";color:teal;font-style:italic'>Timothy&nbsp;
&nbsp;Jones</span></font></i></b></strong><strong><b><i><font
 color=red face=Arial><span style='font-family:Arial;color:red;font-style:
 italic'><o:p></o:p></span></font></i></b></strong></p>
 </td>
 <td width=419 valign=top style='width:314.6pt;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal><strong><b><i><font size=2 color=red face=Arial><span
 style='font-size:10.0pt;font-family:Arial;color:red;font-style:italic'
>Syniverse
 Technologies</span></font></i></b></strong><strong><b><i><font color=teal
 face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC";
 color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p>
 </td>
</tr>
<tr>
 <td width=60 valign=top style='width:44.85pt;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal align=right style='text-align:right'><font size=2
 color=navy face=Arial><span style='font-size:10.0pt;font-family:Arial;
 color:navy'>Work</span></font><strong><b><i><font color=teal
 face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC";
 color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p>
 </td>
 <td width=120 valign=top style='width:1.25in;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
 10.0pt;font-family:Arial;color:navy'>(813) 637-5366<o:p></o:p></span></font><
/p>
 </td>
 <td width=419 valign=top style='width:314.6pt;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
 10.0pt;font-family:Arial;color:navy'>Sr. Systems Engineer&nbsp;&nbsp;</span><
/font><strong><b><font
 color=navy face=Arial><span style='font-family:Arial;color:navy;font-weight:
 normal'><o:p></o:p></span></font></b></strong></p>
 </td>
</tr>
<tr>
 <td width=60 valign=top style='width:44.85pt;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal align=right style='text-align:right'><font size=2
 color=navy face=Arial><span style='font-size:10.0pt;font-family:Arial;
 color:navy'>Cell</span></font><strong><b><i><font color=teal
 face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC";
 color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p>
 </td>
 <td width=120 valign=top style='width:1.25in;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
 10.0pt;font-family:Arial;color:navy'>(813) 857-7650<o:p></o:p></span></font><
/p>
 </td>
 <td width=419 valign=top style='width:314.6pt;padding:0in 5.4pt 0in 5.4pt'>
 <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
 10.0pt;font-family:Arial;color:navy'>Development, <st1:place w:st="on"><st1
:City
  w:st="on">Tampa</st1:City>, <st1:State w:st="on">FL</st1:State></st1:place><
/span></font><strong><b><i><font
 color=teal face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC
";
 color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p>
 </td>
</tr>
</table>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p>&nbsp;</o:p></span></font></p>

</div>

</div>

</body>

</html>