Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display 1 decimal point but round up if the second decimal is 5 or above

I want to display only 1 decimal number and round it to the closest first decimal, but if the second decimal is 5 or above, round up and not down.

For example:

16.55 > 16.6
5.42 > 5.4
23.88 > 23.9

I tried the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT ROUND(time, 1, 1) as time

And it mostly works, except for when the second decimal number is 5 and then it rounds it down, for example 16.55 would round to 16.5

>Solution :

ROUND, according to the documentation has these parameters:

ROUND ( numeric_expression , length [ ,function ] )  

so you have a numeric expression and you want to make it of a certain length by rounding it. The function is described as

Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.

So, if you pass something as a third parameter, then ROUND will effectively truncate rather than round. Your call of

SELECT ROUND(time, 1, 1) as time

passes a third parameter, so you end up with a truncation. Since you wanted rounding instead, avoid passing a third parameter and call the function like this:

SELECT ROUND(time, 1) as time
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading