Oracle SQL - Round decimal to 2/3 based on conditon
up vote
0
down vote
favorite
Hello I have a requirement where I need to round decimals based on the condition:
If the first 2 digit after the decimals is not 00 then I need to round it to 2 place, otherwise 3 places.
Example:
1.232133342 => ROUND(1.232133342,2) //this will work
but if the number is 1.00233231213 value should be 1.002 //round to 3 digit
if the number is 1.0000223123312then the value should be 1.00002
Is there any function? Any help?
sql oracle rounding truncate
add a comment |
up vote
0
down vote
favorite
Hello I have a requirement where I need to round decimals based on the condition:
If the first 2 digit after the decimals is not 00 then I need to round it to 2 place, otherwise 3 places.
Example:
1.232133342 => ROUND(1.232133342,2) //this will work
but if the number is 1.00233231213 value should be 1.002 //round to 3 digit
if the number is 1.0000223123312then the value should be 1.00002
Is there any function? Any help?
sql oracle rounding truncate
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hello I have a requirement where I need to round decimals based on the condition:
If the first 2 digit after the decimals is not 00 then I need to round it to 2 place, otherwise 3 places.
Example:
1.232133342 => ROUND(1.232133342,2) //this will work
but if the number is 1.00233231213 value should be 1.002 //round to 3 digit
if the number is 1.0000223123312then the value should be 1.00002
Is there any function? Any help?
sql oracle rounding truncate
Hello I have a requirement where I need to round decimals based on the condition:
If the first 2 digit after the decimals is not 00 then I need to round it to 2 place, otherwise 3 places.
Example:
1.232133342 => ROUND(1.232133342,2) //this will work
but if the number is 1.00233231213 value should be 1.002 //round to 3 digit
if the number is 1.0000223123312then the value should be 1.00002
Is there any function? Any help?
sql oracle rounding truncate
sql oracle rounding truncate
edited Nov 19 at 8:14
Barmar
414k34239340
414k34239340
asked Nov 19 at 7:46
user3721687
506
506
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
This rounds to at least two digits, maybe more by counting the number of leading zeros in the fractional part using a RegEx
round(x, greatest(2, length(regexp_substr(x,'.0*'))))
see db-fiddle
What about his last condition?if the number is 1.0000223123312then the value should be 1.00002
– Nebi
Nov 19 at 8:33
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
add a comment |
up vote
3
down vote
Subtract the floor of the number from the number, and check if it's less than .01.
CASE WHEN ABS(number) - FLOOR(ABS(number)) < .01 THEN ROUND(number, 3)
ELSE ROUND(number, 2)
END
This fails for negative values
– dnoeth
Nov 19 at 8:27
I addedABS()to fix that.
– Barmar
Nov 19 at 9:21
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
This rounds to at least two digits, maybe more by counting the number of leading zeros in the fractional part using a RegEx
round(x, greatest(2, length(regexp_substr(x,'.0*'))))
see db-fiddle
What about his last condition?if the number is 1.0000223123312then the value should be 1.00002
– Nebi
Nov 19 at 8:33
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
add a comment |
up vote
4
down vote
accepted
This rounds to at least two digits, maybe more by counting the number of leading zeros in the fractional part using a RegEx
round(x, greatest(2, length(regexp_substr(x,'.0*'))))
see db-fiddle
What about his last condition?if the number is 1.0000223123312then the value should be 1.00002
– Nebi
Nov 19 at 8:33
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
This rounds to at least two digits, maybe more by counting the number of leading zeros in the fractional part using a RegEx
round(x, greatest(2, length(regexp_substr(x,'.0*'))))
see db-fiddle
This rounds to at least two digits, maybe more by counting the number of leading zeros in the fractional part using a RegEx
round(x, greatest(2, length(regexp_substr(x,'.0*'))))
see db-fiddle
edited Nov 19 at 9:26
answered Nov 19 at 8:27
dnoeth
44k31838
44k31838
What about his last condition?if the number is 1.0000223123312then the value should be 1.00002
– Nebi
Nov 19 at 8:33
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
add a comment |
What about his last condition?if the number is 1.0000223123312then the value should be 1.00002
– Nebi
Nov 19 at 8:33
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
What about his last condition?
if the number is 1.0000223123312then the value should be 1.00002– Nebi
Nov 19 at 8:33
What about his last condition?
if the number is 1.0000223123312then the value should be 1.00002– Nebi
Nov 19 at 8:33
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
@Nebi: Yep, the question was a bit misleading: round it to 2 place, otherwise 3 place. Fixed ;-)
– dnoeth
Nov 19 at 9:00
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
Yery nice answer! +1
– Nebi
Nov 19 at 9:12
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
@Nebi: Still way too complicated, there's no need for MOD/ABS :-)
– dnoeth
Nov 19 at 9:26
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
Awesome! working fine. thanks.
– user3721687
Nov 19 at 9:39
add a comment |
up vote
3
down vote
Subtract the floor of the number from the number, and check if it's less than .01.
CASE WHEN ABS(number) - FLOOR(ABS(number)) < .01 THEN ROUND(number, 3)
ELSE ROUND(number, 2)
END
This fails for negative values
– dnoeth
Nov 19 at 8:27
I addedABS()to fix that.
– Barmar
Nov 19 at 9:21
add a comment |
up vote
3
down vote
Subtract the floor of the number from the number, and check if it's less than .01.
CASE WHEN ABS(number) - FLOOR(ABS(number)) < .01 THEN ROUND(number, 3)
ELSE ROUND(number, 2)
END
This fails for negative values
– dnoeth
Nov 19 at 8:27
I addedABS()to fix that.
– Barmar
Nov 19 at 9:21
add a comment |
up vote
3
down vote
up vote
3
down vote
Subtract the floor of the number from the number, and check if it's less than .01.
CASE WHEN ABS(number) - FLOOR(ABS(number)) < .01 THEN ROUND(number, 3)
ELSE ROUND(number, 2)
END
Subtract the floor of the number from the number, and check if it's less than .01.
CASE WHEN ABS(number) - FLOOR(ABS(number)) < .01 THEN ROUND(number, 3)
ELSE ROUND(number, 2)
END
edited Nov 19 at 9:21
answered Nov 19 at 8:16
Barmar
414k34239340
414k34239340
This fails for negative values
– dnoeth
Nov 19 at 8:27
I addedABS()to fix that.
– Barmar
Nov 19 at 9:21
add a comment |
This fails for negative values
– dnoeth
Nov 19 at 8:27
I addedABS()to fix that.
– Barmar
Nov 19 at 9:21
This fails for negative values
– dnoeth
Nov 19 at 8:27
This fails for negative values
– dnoeth
Nov 19 at 8:27
I added
ABS() to fix that.– Barmar
Nov 19 at 9:21
I added
ABS() to fix that.– Barmar
Nov 19 at 9:21
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53370305%2foracle-sql-round-decimal-to-2-3-based-on-conditon%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