SQL Value Rounding in the PRINT command

Asked

Viewed 116 times

1

good afternoon.

An Internet exercise asks to calculate how long a given investment can pay a debt, both growing at an interest rate per month.

Observe the created process

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 am displaying the values when I observed the following points :

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

Does anyone have an idea of why?

1 answer

1


If you want the print shows decimal places, must define how decimal some variables 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: Is that the command print can’t tell houses apart decimals when a variable is of the real type or is limited to this variable type.

  • Only one detail, if you ask to display the values of debt and application within the WHILE some will be displayed with decimals and others will not. Some idea?

  • I took the test here and everyone’s gone with decimal places.

  • leaves variables as real or float

  • It will depend on the use, for monetary values I think the best is the decimal.

  • I agree with you. I just wanted to understand the reason for this behavior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.