Casting of numbers with decimal point

3

Good morning to all of you, today I am trying to round a value of FLOAT to two decimals but doing so by consulting SQL SERVER shown below, some values rounded me correctly and others not, example: 19.1 I rounded to 19.10 already with the two digits and 18.4 me it shows it as 18.39 they know what they should or know a better method of doing it, again thanks to all for their help.

SELECT CAST(ROUND(tc_precio, 2, 1) AS DECIMAL (18, 2)) FROM Tipo_Cambio
    
asked by Alberto Arenas 30.11.2016 в 16:34
source

3 answers

4

Actually, you do not even need the ROUND . If you remove it completely, the ROUND is done automatically when casting DECIMAL(18, 2) :

SELECT CAST(tc_precio AS DECIMAL (18, 2))
  FROM Tipo_Cambio
    
answered by 30.11.2016 / 16:57
source
2

You should do it this way:

SELECT CAST(ROUND(tc_cambio, 2) AS DECIMAL (18, 2)) FROM Tipo_Cambio

Only remove the third parameter of ROUND .

    
answered by 30.11.2016 в 16:42
2

Instead of CAST use CONVERT

For example, the following code generates the results 19.10 and 18.40

DECLARE @varFloat1 AS FLOAT
DECLARE @varFloat2 AS FLOAT

SET @varFloat1 = 19.1
SET @varFloat2 = 18.4

SELECT CONVERT(DECIMAL (18, 2), @varFloat1)
SELECT CONVERT(DECIMAL (18, 2), @varFloat2)
    
answered by 30.11.2016 в 16:50