Simple but wierd !!!!

  • Why does this:

    DECLARE @amt money

    set @amt = ROUND(30088.3200 * 0.0055 / 365 * 31, 2)

    print cast(@amt as varchar(38))

    Produce different output than this:

    DECLARE @amt money

    set @amt = 30088.3200 * 0.0055 / 365 * 31

    set @amt = ROUND(@amt, 2)

    print cast(@amt as varchar(38))

    I'm familiar with TypeCasting and have tested this from many standpoints. But - to me they should produce the same results.

  • The second Script is not rounding the value in the variable.

    DECLARE @amt money

    set @amt = 30088.3200 * 0.0055 / 365 * 31

    set @amt = ROUND(@amt, 2)

    print cast(@amt as varchar(38))

    DECLARE @amt money

    set @amt = 30088.3200 * 0.0055 / 365 * 31

    --set @amt = ROUND(@amt, 2)

    print cast(@amt as varchar(38))

    The first script as well as the second script gives the same output.

  • My impression is that the first commands do not restrict the value to be a "money" data type at the time of rounding. The result of the mathematical portion of the equation is 14.054955.

    The first script is rounding this value with a precision of 2 decimal points which does evaluate to 14.05

    The second script converts the value first to a "money" type which results in 14.0550 (4 decimal places). When the round command works on the new value, the output is 14.06.

  • I agree with guarddata, the values are treated as floats until the end when they are implicitly converted to money. Floats is notorious for many issues and really should not be used if you require precision such as when working with money.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply