none
float to decimal conversion: the result is different between SQL Server 2014 and SQL Server 2016 RRS feed

  • Discussione generale

  • Hello to everybody,

    I try to execute the  follwing code on SQL Server 2014 and SQL Server 2016.

    DECLARE @MyFloat float(53) = 512.2
    DECLARE @Digits int = 13
    DECLARE @Display nvarchar(1000);
    DECLARE @MyDecimal decimal(28,15);
    DECLARE @MyDecimal2 decimal(28,15);
    DECLARE @MyRoundedFloat decimal(28,15);
    
    print 'INPUT:				' + CAST(@MyFloat As NVARCHAR(1000))
    set @MyDecimal = CAST(@MyFloat as DECIMAL(28,15));
    print 'CAST:				' + CAST(@MyDecimal as nvarchar(1000));
    set @MyDecimal2 = CONVERT(DECIMAL(28,15), @MyFloat);
    print 'CONVERT:			' + CAST(@MyDecimal2 as nvarchar(1000));
    set @MyRoundedFloat = ROUND(@MyDecimal, @Digits);
    print 'ROUND:				' + CAST(@MyRoundedFloat as nvarchar(1000));
    SET @Display = CAST ( @MyRoundedFloat as nvarchar(1000)) ;

    In SQL 2014 the result is:

    INPUT: 512.2
    CAST: 512.200000000000050
    CONVERT: 512.200000000000050
    ROUND: 512.200000000000100

    In SQL 2016 the result is:

    INPUT: 512.2
    CAST: 512.200000000000045
    CONVERT: 512.200000000000045
    ROUND: 512.200000000000000

    Why I get these 2 different behaviors?


    giovedì 30 marzo 2017 13:42