简体   繁体   中英

Round Up Float column to next number with a multiple of 5

Currently I have a column defined as a float(ex 16.98) I need to be able to take the value and find the next highest multiple of 5.

Examples...

Value 16.98           Should Return 17.00  
Value 10.46           Should Return 10.50    
Value  9.11           Should Return  9.15  
Value  8.10           Should Return  8.10  
Value  18.65          Should Return 18.65  

Notice that if is is a multiple of 5 then it should return itself.

SELECT Format((ceiling(cast(unitprice*100 as int)/5.0)*5.0)/100.0, 'g18')
From table

If You are using MySql try this:

SELECT (ceil(cast(val*100 as signed)/5.0)*5.0)/100.0 from tbl;

CAST inside CEIL is very helpful - it eliminates rounding error.

Here is Demo

In SQL SERVER:

SELECT (ceiling(cast(val*100 as int)/5.0)*5.0)/100.0 from tbl;

Where tbl contains values to transform.

Try this:

SELECT
CEILING(16.98 * 20) / 20,
CEILING(10.46 * 20) / 20,
CEILING(9.11 * 20) / 20,
CEILING(8.10 * 20) / 20,
CEILING(18.65 * 20) / 20

So the query would look like:

SELECT CEILING(yourColumn * 20) / 20 AS Result FROM yourTable;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM