MySQL-如何始终取整小数? [英] MySQL - How can I always round up decimals?

查看:85
本文介绍了MySQL-如何始终取整小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我具有以下值:

0.000018

这是6个小数位,但是我想将其四舍五入到最接近的小数点后第4位,这样:

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

0.000018 -> 0.0001

我玩过round()函数,但是如果我只是简单地使用round函数:

I've played with the round() funcction but if I simply use the round function:

round(0.000018,4) = 0.0000

在处理财务用小数时,在这种情况下,需要四舍五入并向客户收费,而不是给他们免费赠品!但是round()会根据值向上或向下取整,我需要始终如一地向上取整.

When dealing with decimals for financial purposes, in this case one needs to round up and charge the customer instead of giving them a freebie! But round() will go round up or down depending on value, I need to consistently round up.

是否有一种简单的方法?

Is there a simple way of doing this?

推荐答案

您可以使用

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.

所以ceil(0.000145* 10000) = ceil(1.45) = 2 分开,您将得到0.0002

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

等等,会吗?那是行不通的.我的意思是显然FLOOR,但工作是相同的:D 手册也在同一页上:)

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 :)

所以floor(0.000145* 10000) = floor(1.45) = 1 分开,您将得到0.0001

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

这篇关于MySQL-如何始终取整小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆