Help on my function to get a percentual from two values

Hello guys

I made this function to get the percentual from two value, but sometimes it works fine and sometimes it doesn't.

could you help me to ajust it?

Thanks a lot

alter Function  fn_PercentualDesconto (@Fullvalue float, @EndValue float)
  Returns Float
Begin 
	declare @v01 float;
	declare @v02 float;
	declare @v03 float;

        Set @v01=@Fullvalue 
	Set @v02=@EndValue 
	Set @v03=(@v01-@v02)
	Return 100- round((@v03 / @v01 ) * 100 , 2) 
End

Open in new window

LVL 1
hidrauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
replace Return 100- round((@v03 / @v01 ) * 100 , 2)
with
Return 100.00- round((@v03 / @v01 ) * 100.00, 2)

Open in new window

100 is an integer
0
pcelbaCommented:
If you are sure the @Fullvalue can never be 0 then calculate the result as
alter Function  fn_PercentualDesconto (@Fullvalue float, @EndValue float)
  Returns Float
Begin 
	Return round( (@EndValue / @Fullvalue) * 100, 2) 
End

Open in new window

If the possibility to have @Fullvalue = 0.0 exists then add one condition:
Return CASE WHEN @Fullvalue = 0.0 THEN 0.00 ELSE round( (@EndValue / @Fullvalue) * 100, 2) END

Open in new window

Or generate some error.

BTW, I don't understand your sentence "sometimes it works fine and sometimes it doesn't." .  Could you provide some values which do not work?
0
hidrauAuthor Commented:
Hello Guys,

Thanks a lot for helping me

But Something wrong here, take a look:

When I do this:

select dbo.fn_PercentualDesconto(60, 54)

it returns me 90, it is wrong because it should be 10%  

60 is my full value and I reached the 54 - The discount that I have was 6

6 on the 60 represents 10% and not 90%

thanks
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Steve BinkCommented:
        Set @v01=@Fullvalue 
	Set @v02=@EndValue 
	Set @v03=(@v01-@v02)
	Return 100- round((@v03 / @v01 ) * 100 , 2) 

Open in new window


Executes as:
Set @v01 = 60
Set @v02 = 54
Set @v03 = (60-54)   <--  @v03=6
Return 100 - round (6 / 60 * 100, 2)   <-- return 100 - 10, or 90

Open in new window

Sounds like your code is doing exactly what you told it to.
0
hidrauAuthor Commented:
Steve Bink,

I think my code is not correct.

See:

I bought a book for $ 100, its normal price was $ 112.

When I was going to pay it, I just payed $100 becouse I had a discount on it.

It is not working at any way :(
What was my discount?

Select dbo.fn_PercentualDesconto(112, 100)

The function returned me: %89,29   and it is not correct, it should be 10% of discount.
0
Steve BinkCommented:
The formula for percent discount is (1 - (discount_price / full_price)).  The formula being used in your code is (1 - ((full_price - discount_price) / full_price)).  Remove line 10 from your original code, and use @v02 instead of @v03 in the return calculation.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hidrauAuthor Commented:
thanks guys for the help
0
pcelbaCommented:
So you now understand percent value vs. percent discount hopefully.

You could simply use my formula slightly updated for discount calculation:

Return round( (1-(@EndValue / @Fullvalue)) * 100, 2)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.