Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2007, 03:55 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Roudning Issue

Hi,

In my user defined function I am using code like this to get a number with 2 decimal places:

if @reportCurrency=1
   begin
     set @calculatedRate = @fltAdRate + ISNULL(@fltPosRate,0)
   end

if @reportCurrency=2
   begin
     set @calculatedRate = (@fltAdRate + ISNULL(@fltPosRate,0))- (@fltAdRate + ISNULL(@fltPosRate,0)) * IsNull(@agencyDiscount,15) / 100
   end
if @reportCurrency=3
   begin
     set @calculatedRate = (@fltAdRate + ISNULL(@fltPosRate,0))-(@fltAdRate + ISNULL(@fltPosRate,0)) * (IsNull(@agencyDiscount,15) / 100)
     set @calculatedRate = (@calculatedRate)- (@calculatedRate) * (IsNull(@cashDiscount,0) /100)
end

Then I cast the @calculatedRate

Set @returnVal = ISNULL(CAST(@calculatedRate AS DECIMAL(15,2)), -1)

if @returnVal = -1.00
     SET @returnVal = CAST (@returnVal AS DECIMAL(15,0))


RETURN @returnVal

The problem is that it rounds the number. Example:

Gross amount $2415 net down 15% = $2052.75 Not $2053.00

AS you see the .75 cents is rounded to 2053.00. I would like to keep the .75 cents like $2052.75

I would appreciate your help.

Cheers
 
Old July 11th, 2007, 07:06 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

This is a simpler example of my problem. I am using query analyser to test.

It seems that if I change the return type of the user defined function to float and decalre all the variables to be float it gives the right result.

The only problem now is when I test for null. If it is null I have to return -1.

But it is returning -1.0

How can get rid of the .0 and return just -1. I have tried to cast to int but to no avail.

DECLARE @calculatedRate float
DECLARE @returnVal float

DECLARE @fltAdRate float
SET @fltAdRate = 2415.00

DECLARE @fltPosRate float
SET @fltPosRate = NULL

DECLARE @cashDiscount float
SET @cashDiscount = 0.0

DECLARE @agencyDiscount float
SET @agencyDiscount = 15.0

set @calculatedRate = (@fltAdRate + ISNULL(@fltPosRate,0))- (@fltAdRate + ISNULL(@fltPosRate,0)) * @agencyDiscount / 100

Set @returnVal = ISNULL(CAST(@calculatedRate AS DECIMAL(15,2)), -1)

SET @returnVal = -1

if @returnVal = -1 or @returnVal = -1.0 or @returnVal = -1.00
     --SET @returnVal = CAST (@returnVal AS DECIMAL(15,0))
    SET @returnVal = CAST (@returnVal AS int)

select @returnVal as returnVal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Com issue(really urgent) balesh.mind ASP.NET 2.0 Professional 2 May 14th, 2008 03:35 AM
Drop Down Issue SKhna ASP.NET 2.0 Basics 1 March 19th, 2008 06:29 AM
Issue with DTD tgopal CSS Cascading Style Sheets 2 September 20th, 2006 04:15 AM
Another issue islandtiu BOOK: Beginning ASP 3.0 1 February 14th, 2005 11:49 AM
Sub Report Issue bjoneskc01 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 January 21st, 2005 11:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.