Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT a FLOAT with given precision

I want to store my values as FLOATS, but when retrieving them the user can specify (and change at runtime) how many digits he wants to see after the decimal point.

Can I pass that in my SELECT somehow, or do I have to do it in my application?

like image 948
Mawg says reinstate Monica Avatar asked Nov 29 '12 07:11

Mawg says reinstate Monica


2 Answers

Yes, just specify the second parameter for ROUND()

SELECT ROUND(23.298, <precision>);

With this, you can specify the number of digits you would like to get returned. (you can even have negative values, in wither case, the input gets rounded to 10(-precision)).

Or you could use CAST as DECIMAL:

 CAST(2.5 AS DECIMAL(20,2))

(Note: this latter would work with textual inputs too, like CAST('2.5' AS DECIMAL(20,2)), but in this case it is about FLOAT inputs)

like image 157
ppeterka Avatar answered Oct 12 '22 23:10

ppeterka


use ROUND(), CEIL() or FLOOR() to do the same and get the second argument's value passed from user as an input.

like image 42
Ajith Sasidharan Avatar answered Oct 13 '22 00:10

Ajith Sasidharan