简体   繁体   中英

Oracle SQL - Round decimal to 2/3 based on conditon

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.0000223123312 then the value should be 1.00002

Is there any function? Any help?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM