MySQL round half

后端 未结 4 1054
时光取名叫无心
时光取名叫无心 2021-01-23 14:27

Is it possible, in MySQL, to round half a specific way like PHP would do?

  • PHP_ROUND_HALF_UP
  • PHP_ROUND_HALF_DOWN
  • P
4条回答
  •  一整个雨季
    2021-01-23 14:50

    Rounding on half specifically can be done using the ROUND function in just a single line:

    SELECT ROUND(ROUND({your_input_value}*2,0)/2,1);
    
    • The first ROUND({your_input_value}*2,0) gives you a rounded value as a whole integer.
    • The second ROUND(.../2,1) gives you a single digit result instead of a bit longer result with more zeros and possible floating point deviations.

    Similar other rounds with round up, down, or other multipliers like 3,4, etc can be created.

提交回复
热议问题