mysqlsqldatabase

Rounding up to the nearest .49 or .99


I want an SQL query to round up to the nearest .49 or .99

I currently have it written to round up to .99 no problems, using the FLOOR or TRUNCATE function

UPDATE wp_postmeta 
SET meta_value=meta_value*1.39
WHERE meta_key='_saleprice' OR meta_key='_price';
UPDATE wp_postmeta
SET meta_value = FLOOR(meta_value) + 0.99
WHERE meta_key='_price' OR meta_key='_saleprice’;

this first of all multiplies the price by 1.39 then FLOORS this (removed after the decimal rounding down to nearest whole number) then adds the 0.99 on to the end... Effecitvely in total this rounds up to the nearest 0.99

PRICE AFTER *1.39          PRICE AFTER ROUNDING
10.00                                                10.99
10.49                                                10.99
10.50                                                10.99
10.99                                                10.99
11.00                                                11.99
11.49                                                11.99
11.50                                                11.99
11.99                                                11.99

what I want to achieve is

PRICE AFTER *1.39          PRICE AFTER ROUNDING
10.00                                                10.49
10.49                                                10.49
10.50                                                10.99
10.99                                                10.99
11.00                                                11.49
11.49                                                11.49
11.50                                                11.99
11.99                                                11.99

How am I best going about achieving this?


Solution

  • The following should do the trick:

    * 2.0, trunc down, divide by 2, add 0.49