How to round the sum of node values to 2 decimals?
I'm creating pdf from xml and xslt files with Apache FOP 2.3.
I have the following XML:
<root>
<operations>
<operation>
<sold>5800.00</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>0.00</sold>
</operation>
</operations>
</root>
In the xslt file I have the following line:
<xsl:value-of select="sum(.//operation/sold[number(.) = .])"/>
This will calculate the sum of all sold
elements, resulting in 11297.960000000001
(the correct result would be 11297.96
). I need it to be rounded to 2 decimals, like this: 11297.96
. Also if the if the second decimal is 0, I still want to display it, so if the sum was, for example 12.3
, I want 12.30
to appear.
xml xslt rounding
add a comment |
I'm creating pdf from xml and xslt files with Apache FOP 2.3.
I have the following XML:
<root>
<operations>
<operation>
<sold>5800.00</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>0.00</sold>
</operation>
</operations>
</root>
In the xslt file I have the following line:
<xsl:value-of select="sum(.//operation/sold[number(.) = .])"/>
This will calculate the sum of all sold
elements, resulting in 11297.960000000001
(the correct result would be 11297.96
). I need it to be rounded to 2 decimals, like this: 11297.96
. Also if the if the second decimal is 0, I still want to display it, so if the sum was, for example 12.3
, I want 12.30
to appear.
xml xslt rounding
1
Use theformat-number()
function: w3.org/TR/1999/REC-xslt-19991116#format-number
– michael.hor257k
Jan 1 at 18:08
2
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or latersum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can useformat-number
e.g.format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
– Martin Honnen
Jan 1 at 18:09
Thanks, this works. What difference dose it make to use the decimal(.)? What is the difference between the result offormat-number(sum(.//operation/sold/xs:decimal(.))
andformat-number(sum(.//operation/sold)
?
– mpontsasa
Jan 1 at 20:07
add a comment |
I'm creating pdf from xml and xslt files with Apache FOP 2.3.
I have the following XML:
<root>
<operations>
<operation>
<sold>5800.00</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>0.00</sold>
</operation>
</operations>
</root>
In the xslt file I have the following line:
<xsl:value-of select="sum(.//operation/sold[number(.) = .])"/>
This will calculate the sum of all sold
elements, resulting in 11297.960000000001
(the correct result would be 11297.96
). I need it to be rounded to 2 decimals, like this: 11297.96
. Also if the if the second decimal is 0, I still want to display it, so if the sum was, for example 12.3
, I want 12.30
to appear.
xml xslt rounding
I'm creating pdf from xml and xslt files with Apache FOP 2.3.
I have the following XML:
<root>
<operations>
<operation>
<sold>5800.00</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>422.92</sold>
</operation>
<operation>
<sold>0.00</sold>
</operation>
</operations>
</root>
In the xslt file I have the following line:
<xsl:value-of select="sum(.//operation/sold[number(.) = .])"/>
This will calculate the sum of all sold
elements, resulting in 11297.960000000001
(the correct result would be 11297.96
). I need it to be rounded to 2 decimals, like this: 11297.96
. Also if the if the second decimal is 0, I still want to display it, so if the sum was, for example 12.3
, I want 12.30
to appear.
xml xslt rounding
xml xslt rounding
edited Jan 1 at 21:18
marc_s
581k13011211268
581k13011211268
asked Jan 1 at 18:02
mpontsasampontsasa
33
33
1
Use theformat-number()
function: w3.org/TR/1999/REC-xslt-19991116#format-number
– michael.hor257k
Jan 1 at 18:08
2
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or latersum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can useformat-number
e.g.format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
– Martin Honnen
Jan 1 at 18:09
Thanks, this works. What difference dose it make to use the decimal(.)? What is the difference between the result offormat-number(sum(.//operation/sold/xs:decimal(.))
andformat-number(sum(.//operation/sold)
?
– mpontsasa
Jan 1 at 20:07
add a comment |
1
Use theformat-number()
function: w3.org/TR/1999/REC-xslt-19991116#format-number
– michael.hor257k
Jan 1 at 18:08
2
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or latersum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can useformat-number
e.g.format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
– Martin Honnen
Jan 1 at 18:09
Thanks, this works. What difference dose it make to use the decimal(.)? What is the difference between the result offormat-number(sum(.//operation/sold/xs:decimal(.))
andformat-number(sum(.//operation/sold)
?
– mpontsasa
Jan 1 at 20:07
1
1
Use the
format-number()
function: w3.org/TR/1999/REC-xslt-19991116#format-number– michael.hor257k
Jan 1 at 18:08
Use the
format-number()
function: w3.org/TR/1999/REC-xslt-19991116#format-number– michael.hor257k
Jan 1 at 18:08
2
2
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later
sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.– Martin Honnen
Jan 1 at 18:09
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later
sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.– Martin Honnen
Jan 1 at 18:09
Thanks, this works. What difference dose it make to use the decimal(.)? What is the difference between the result of
format-number(sum(.//operation/sold/xs:decimal(.))
and format-number(sum(.//operation/sold)
?– mpontsasa
Jan 1 at 20:07
Thanks, this works. What difference dose it make to use the decimal(.)? What is the difference between the result of
format-number(sum(.//operation/sold/xs:decimal(.))
and format-number(sum(.//operation/sold)
?– mpontsasa
Jan 1 at 20:07
add a comment |
1 Answer
1
active
oldest
votes
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
The xs:decimal
data type offers a higher precision than the xs:double
(respectively number in XPath 1) data type, the inaccuracy that you see is just how the IEEE defines the IEEE double-precision 64-bit floating point type that XSLT/XPath 2 and later inherit from the XSD schema language https://www.w3.org/TR/xmlschema-2/#double. You will find the same problem in other programming languages using that data type (e.g. Javascript http://jsfiddle.net/23g5wexz/).
The sum(.//operation/sold)
will work with the xs:double
data type (https://www.w3.org/TR/xpath-functions/#func-sum), if you know you use format-number
anyway on the result it suffices to work with the default xs:double
(I think that default is mainly used in XSLT/XPath 2 and later for backwards compatibility with XSLT/XPath where a single number data type exists that implements/follows the IEEE double-precision 64-bit floating point spec.).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997709%2fhow-to-round-the-sum-of-node-values-to-2-decimals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
The xs:decimal
data type offers a higher precision than the xs:double
(respectively number in XPath 1) data type, the inaccuracy that you see is just how the IEEE defines the IEEE double-precision 64-bit floating point type that XSLT/XPath 2 and later inherit from the XSD schema language https://www.w3.org/TR/xmlschema-2/#double. You will find the same problem in other programming languages using that data type (e.g. Javascript http://jsfiddle.net/23g5wexz/).
The sum(.//operation/sold)
will work with the xs:double
data type (https://www.w3.org/TR/xpath-functions/#func-sum), if you know you use format-number
anyway on the result it suffices to work with the default xs:double
(I think that default is mainly used in XSLT/XPath 2 and later for backwards compatibility with XSLT/XPath where a single number data type exists that implements/follows the IEEE double-precision 64-bit floating point spec.).
add a comment |
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
The xs:decimal
data type offers a higher precision than the xs:double
(respectively number in XPath 1) data type, the inaccuracy that you see is just how the IEEE defines the IEEE double-precision 64-bit floating point type that XSLT/XPath 2 and later inherit from the XSD schema language https://www.w3.org/TR/xmlschema-2/#double. You will find the same problem in other programming languages using that data type (e.g. Javascript http://jsfiddle.net/23g5wexz/).
The sum(.//operation/sold)
will work with the xs:double
data type (https://www.w3.org/TR/xpath-functions/#func-sum), if you know you use format-number
anyway on the result it suffices to work with the default xs:double
(I think that default is mainly used in XSLT/XPath 2 and later for backwards compatibility with XSLT/XPath where a single number data type exists that implements/follows the IEEE double-precision 64-bit floating point spec.).
add a comment |
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
The xs:decimal
data type offers a higher precision than the xs:double
(respectively number in XPath 1) data type, the inaccuracy that you see is just how the IEEE defines the IEEE double-precision 64-bit floating point type that XSLT/XPath 2 and later inherit from the XSD schema language https://www.w3.org/TR/xmlschema-2/#double. You will find the same problem in other programming languages using that data type (e.g. Javascript http://jsfiddle.net/23g5wexz/).
The sum(.//operation/sold)
will work with the xs:double
data type (https://www.w3.org/TR/xpath-functions/#func-sum), if you know you use format-number
anyway on the result it suffices to work with the default xs:double
(I think that default is mainly used in XSLT/XPath 2 and later for backwards compatibility with XSLT/XPath where a single number data type exists that implements/follows the IEEE double-precision 64-bit floating point spec.).
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can use format-number
e.g. format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.
The xs:decimal
data type offers a higher precision than the xs:double
(respectively number in XPath 1) data type, the inaccuracy that you see is just how the IEEE defines the IEEE double-precision 64-bit floating point type that XSLT/XPath 2 and later inherit from the XSD schema language https://www.w3.org/TR/xmlschema-2/#double. You will find the same problem in other programming languages using that data type (e.g. Javascript http://jsfiddle.net/23g5wexz/).
The sum(.//operation/sold)
will work with the xs:double
data type (https://www.w3.org/TR/xpath-functions/#func-sum), if you know you use format-number
anyway on the result it suffices to work with the default xs:double
(I think that default is mainly used in XSLT/XPath 2 and later for backwards compatibility with XSLT/XPath where a single number data type exists that implements/follows the IEEE double-precision 64-bit floating point spec.).
answered Jan 1 at 20:41
Martin HonnenMartin Honnen
112k66279
112k66279
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997709%2fhow-to-round-the-sum-of-node-values-to-2-decimals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use the
format-number()
function: w3.org/TR/1999/REC-xslt-19991116#format-number– michael.hor257k
Jan 1 at 18:08
2
Depending on the XSLT version and processor you can avoid the double inaccuracy by summing decimals in XSLT/XPath 2 or later
sum(.//operation/sold/xs:decimal(.))
, for formatting any number as needed you can useformat-number
e.g.format-number(sum(.//operation/sold/xs:decimal(.)), '0.00')
.– Martin Honnen
Jan 1 at 18:09
Thanks, this works. What difference dose it make to use the decimal(.)? What is the difference between the result of
format-number(sum(.//operation/sold/xs:decimal(.))
andformat-number(sum(.//operation/sold)
?– mpontsasa
Jan 1 at 20:07