MySQL - How can I always round up decimals?

前端 未结 3 500
悲&欢浪女
悲&欢浪女 2020-12-16 13:26

For instance I have the following value:

0.000018

This is 6 decimal places, but I want to round it up the nearest whole 4th decimal place, so th

相关标签:
3条回答
  • 2020-12-16 13:57

    Use ROUND(X,D), which rounds the value X to D decimal places.

    0 讨论(0)
  • 2020-12-16 14:04

    You can use ceil (ceiling). It only rounds up, so you'll have to multiply with 10000, do the ceil and then divide the result again.

    So ceil(0.000145* 10000) = ceil(1.45) = 2 Divide back and you'll have 0.0002

    EDIT: wait, wut? that doesn't work. I mean FLOOR obviously but the working is the same :D The manual is on the same page too :)

    So floor(0.000145* 10000) = floor(1.45) = 1 Divide back and you'll have 0.0001

    0 讨论(0)
  • 2020-12-16 14:16

    There's another method, which is to add half a multiple of 10. For example: round(x+0.005, 2) where x is 0.923 = 0.93 which reduces the maximum floating point division error.

    0 讨论(0)
提交回复
热议问题