Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding the value to the nearest 50

I am trying to round down the value to the nearest 50. 1-50 it should round down to below 00 and when its 51-rest then it should round down to 50

ex:

  • 245 (until 1-49) its should round down to 200
  • 258 (from 50-99)then it should round down to 250

I tried this,its wrking good but I need smething other than case statement

@ResultAmount = ROUND(@ResultAmount, -2, 1) + 
    CASE    WHEN RIGHT(CONVERT(INT, FLOOR(@ResultAmount)), 2) IN (00, 50)
            THEN RIGHT(CONVERT(INT, FLOOR(@ResultAmount)), 2)
            WHEN RIGHT(CONVERT(INT, FLOOR(@ResultAmount)), 2) BETWEEN 1 AND 49
            THEN 00
            WHEN RIGHT(CONVERT(INT, FLOOR(@ResultAmount)), 2) BETWEEN 51 AND 99
            THEN 50
            END

Thanks in advance!!!

like image 306
kris Avatar asked Dec 29 '25 00:12

kris


2 Answers

This is all you need

SELECT FLOOR(@ResultAmount / 50) * 50;

e.g below

declare @ResultAmount decimal(10,2) = 249;

SELECT FLOOR(@ResultAmount / 50) * 50;

SET @ResultAmount = 250;

SELECT FLOOR(@ResultAmount / 50) * 50;

SET @ResultAmount = 200;

SELECT FLOOR(@ResultAmount / 50) * 50;

SET @ResultAmount = 199;

SELECT FLOOR(@ResultAmount / 50) * 50;
like image 91
Cato Avatar answered Dec 30 '25 18:12

Cato


It sounds like numbers 0-50 get rounded up to "50", but any number larger than that should just get rounded to the nearest 50. Something like the following should work:

(CASE WHEN f1/50 < 1 THEN 1 ELSE ceiling(f1/50) END) * 50 AS rounded_to_50
like image 21
JNevill Avatar answered Dec 30 '25 18:12

JNevill