Rounding SQL Values in PRINT Command

1

Good afternoon.

An internet exercise asks you to calculate how much time a given investment can pay a debt, both growing at an interest rate per month.

Notice the procedure created

declare @divida real = 10000
declare @aplicacao real = 1500
declare @taxa_div real = 0.025
declare @taxa_apli real = 0.04
declare @meses int = 1

while (@divida > @aplicacao) begin
    set @divida = (@divida * @taxa_div) + @divida
    set @aplicacao = (@aplicacao * @taxa_apli) + @aplicacao
    set @meses = @meses + 1

end
print @meses
select @divida
select @aplicacao
print @divida
print @aplicacao
print @aplicacao - @divida

At the end I'm displaying the values when I noticed the following points:

  • At the end when I ask to display the values with the PRINT is not displayed with decimal places already, using the SELECT the decimal places are displayed.
  • When I ask you to subtract the values, the result is displayed with decimal places.

Does anyone have an idea of the pq?

    
asked by anonymous 14.01.2016 / 18:42

1 answer

1

If you want print to show the decimal places, you should set some variables to decimal , it would look like this:

declare @divida decimal(12,4) = 10000
declare @aplicacao decimal(12,4) = 1500
declare @taxa_div real = 0.025
declare @taxa_apli real = 0.04
declare @meses int = 1

while (@divida > @aplicacao) begin
    set @divida = (@divida * @taxa_div) + @divida
    set @aplicacao = (@aplicacao * @taxa_apli) + @aplicacao
    set @meses = @meses + 1

end
print @meses
select @divida
select @aplicacao
print @divida
print @aplicacao
print @aplicacao - @divida
  

132

     

253995.5781

     

(1 row (s) affected)

     

255539.8906

     

(1 row (s) affected)

     

253995.5781
  255539.8906
  1544.3125

     

My opinion: The print command can not distinguish houses   decimal places when a variable is of the real type or is limited to this   type of variable.

    
14.01.2016 / 22:13