Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

rounding - MySQL - How can I always round up decimals?

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

0.000018 -> 0.0001

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

round(0.000018,4) = 0.0000

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...