SQL round off issueShould you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Parameterize an SQL IN clauseInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL table
I have accepted an internship offer. Should I inform companies I have applied to that have not gotten back to me yet?
Why is "dark" an adverb in this sentence?
What made Windows ME so crash-prone?
What do these three diagonal lines that cross through three measures and both staves mean, and what are they called?
When do sending stones recharge if they are in different time zones (or different planes)?
Why should I cook the flour first when making bechamel sauce?
Why limit to revolvers?
Re-negotiate salary once I earn my diploma
What is the superlative of ipse?
Do First Order blasters maintain a record of when they were fired?
Is `curl something | sudo bash -` a reasonably safe installation method?
Why did Steve Rogers choose Sam in Endgame?
What impact would a dragon the size of Asia have on the environment?
Manually select/unselect lines before forwarding to stdout
How to unload a Mathematica package?
I gave my characters names that are exactly like another book. Is it a problem?
What do mathematicians mean when they say some conjecture can’t be proven using the current technology?
Clarification on defining FFT bin sizes
Construct a pentagon avoiding compass use
When does Fisher's "go get more data" approach make sense?
Using print composer name in a label
What's the meaning of こそ in this sentence?
Why does the Trade Federation become so alarmed upon learning the ambassadors are Jedi Knights?
Diminished chord constructed over the tonic degree?
SQL round off issue
Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Parameterize an SQL IN clauseInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Consider these values which are of type MONEY (sample values and these can change)
select 4796.529 + 1585.0414 + 350.9863 + 223.3549 + 127.6314+479.6529 + 158.5041
for some reason I need to round each value to a scale of 3 like this
select round(4796.529,3)+ round(1585.0414,3)+ round(350.9863,3)+ round(223.3549,3)+ round(127.6314,3)+ round(479.6529,3)+ round(158.5041,3)
but when I take the sum they shows a very minor variation. first line of code returns 7721.7000. and the second one 7721.6990. But this variation in not acceptable. What is the best way to solve this ?
sql
add a comment |
Consider these values which are of type MONEY (sample values and these can change)
select 4796.529 + 1585.0414 + 350.9863 + 223.3549 + 127.6314+479.6529 + 158.5041
for some reason I need to round each value to a scale of 3 like this
select round(4796.529,3)+ round(1585.0414,3)+ round(350.9863,3)+ round(223.3549,3)+ round(127.6314,3)+ round(479.6529,3)+ round(158.5041,3)
but when I take the sum they shows a very minor variation. first line of code returns 7721.7000. and the second one 7721.6990. But this variation in not acceptable. What is the best way to solve this ?
sql
1
"The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent. For Informatica, the money and smallmoney data types are accurate to a one-hundredth of the monetary units that they represent."
– Zohar Peled
Mar 26 at 7:25
mathematics instead of technical, i believe. so just follow the way business requires.
– Rex
Mar 26 at 7:28
add a comment |
Consider these values which are of type MONEY (sample values and these can change)
select 4796.529 + 1585.0414 + 350.9863 + 223.3549 + 127.6314+479.6529 + 158.5041
for some reason I need to round each value to a scale of 3 like this
select round(4796.529,3)+ round(1585.0414,3)+ round(350.9863,3)+ round(223.3549,3)+ round(127.6314,3)+ round(479.6529,3)+ round(158.5041,3)
but when I take the sum they shows a very minor variation. first line of code returns 7721.7000. and the second one 7721.6990. But this variation in not acceptable. What is the best way to solve this ?
sql
Consider these values which are of type MONEY (sample values and these can change)
select 4796.529 + 1585.0414 + 350.9863 + 223.3549 + 127.6314+479.6529 + 158.5041
for some reason I need to round each value to a scale of 3 like this
select round(4796.529,3)+ round(1585.0414,3)+ round(350.9863,3)+ round(223.3549,3)+ round(127.6314,3)+ round(479.6529,3)+ round(158.5041,3)
but when I take the sum they shows a very minor variation. first line of code returns 7721.7000. and the second one 7721.6990. But this variation in not acceptable. What is the best way to solve this ?
sql
sql
asked Mar 26 at 7:20
Vishnu BabuVishnu Babu
63810 silver badges24 bronze badges
63810 silver badges24 bronze badges
1
"The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent. For Informatica, the money and smallmoney data types are accurate to a one-hundredth of the monetary units that they represent."
– Zohar Peled
Mar 26 at 7:25
mathematics instead of technical, i believe. so just follow the way business requires.
– Rex
Mar 26 at 7:28
add a comment |
1
"The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent. For Informatica, the money and smallmoney data types are accurate to a one-hundredth of the monetary units that they represent."
– Zohar Peled
Mar 26 at 7:25
mathematics instead of technical, i believe. so just follow the way business requires.
– Rex
Mar 26 at 7:28
1
1
"The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent. For Informatica, the money and smallmoney data types are accurate to a one-hundredth of the monetary units that they represent."
– Zohar Peled
Mar 26 at 7:25
"The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent. For Informatica, the money and smallmoney data types are accurate to a one-hundredth of the monetary units that they represent."
– Zohar Peled
Mar 26 at 7:25
mathematics instead of technical, i believe. so just follow the way business requires.
– Rex
Mar 26 at 7:28
mathematics instead of technical, i believe. so just follow the way business requires.
– Rex
Mar 26 at 7:28
add a comment |
2 Answers
2
active
oldest
votes
When you round numbers before you sum them you will get a different result than if you round numbers after you have summed them. Simple as that. There is no way to solve this.
add a comment |
As Whencesoever said, your problem is mathmatical one, not a programming error.
- 12.5 + 11.6 = 24.1
- ROUND(12.5) + ROUND(11.6) = 25
- ROUND(12.5 + 11.6) = 24
I'd talk with the business and figure out where they want the rounding applied.
Also, as a side note, MONEY is a terrible datatype. If you can, you may want to consider switching to a DECIMAL. See Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
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%2f55351685%2fsql-round-off-issue%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you round numbers before you sum them you will get a different result than if you round numbers after you have summed them. Simple as that. There is no way to solve this.
add a comment |
When you round numbers before you sum them you will get a different result than if you round numbers after you have summed them. Simple as that. There is no way to solve this.
add a comment |
When you round numbers before you sum them you will get a different result than if you round numbers after you have summed them. Simple as that. There is no way to solve this.
When you round numbers before you sum them you will get a different result than if you round numbers after you have summed them. Simple as that. There is no way to solve this.
edited Mar 26 at 7:28
Dale Burrell
4,1785 gold badges27 silver badges57 bronze badges
4,1785 gold badges27 silver badges57 bronze badges
answered Mar 26 at 7:26
WhencesoeverWhencesoever
1,6818 silver badges19 bronze badges
1,6818 silver badges19 bronze badges
add a comment |
add a comment |
As Whencesoever said, your problem is mathmatical one, not a programming error.
- 12.5 + 11.6 = 24.1
- ROUND(12.5) + ROUND(11.6) = 25
- ROUND(12.5 + 11.6) = 24
I'd talk with the business and figure out where they want the rounding applied.
Also, as a side note, MONEY is a terrible datatype. If you can, you may want to consider switching to a DECIMAL. See Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
add a comment |
As Whencesoever said, your problem is mathmatical one, not a programming error.
- 12.5 + 11.6 = 24.1
- ROUND(12.5) + ROUND(11.6) = 25
- ROUND(12.5 + 11.6) = 24
I'd talk with the business and figure out where they want the rounding applied.
Also, as a side note, MONEY is a terrible datatype. If you can, you may want to consider switching to a DECIMAL. See Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
add a comment |
As Whencesoever said, your problem is mathmatical one, not a programming error.
- 12.5 + 11.6 = 24.1
- ROUND(12.5) + ROUND(11.6) = 25
- ROUND(12.5 + 11.6) = 24
I'd talk with the business and figure out where they want the rounding applied.
Also, as a side note, MONEY is a terrible datatype. If you can, you may want to consider switching to a DECIMAL. See Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?
As Whencesoever said, your problem is mathmatical one, not a programming error.
- 12.5 + 11.6 = 24.1
- ROUND(12.5) + ROUND(11.6) = 25
- ROUND(12.5 + 11.6) = 24
I'd talk with the business and figure out where they want the rounding applied.
Also, as a side note, MONEY is a terrible datatype. If you can, you may want to consider switching to a DECIMAL. See Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?
answered Mar 26 at 7:37
MooseMoose
1709 bronze badges
1709 bronze badges
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
add a comment |
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
if they're simply looking for a rounded total, then perhaps summing them up and rounding the result is what they're actually looking for instead of rounding each of the numbers individually?
– user2366842
Mar 27 at 2:27
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%2f55351685%2fsql-round-off-issue%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
"The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent. For Informatica, the money and smallmoney data types are accurate to a one-hundredth of the monetary units that they represent."
– Zohar Peled
Mar 26 at 7:25
mathematics instead of technical, i believe. so just follow the way business requires.
– Rex
Mar 26 at 7:28