Hack Forums
[.NET] [VB.NET]Get Percent Value - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET]Get Percent Value (/showthread.php?tid=400883)



[VB.NET]Get Percent Value - codepoet - May 3, 2010

Description:

Calculates the Percentvalue of a given value depending on a maximum value.

Code
Public Function Percent(ByVal CurrVal As Long, ByVal MaxVal As Long) As Integer
    Return Int(CurrVal / MaxVal * 100 + 0.5)
End Function

originally posted on dotnet-snippets.com

[img]http://i685.photobucket.com/albums/vv215/blackpearl4/THANKS.gif[/img]

If You Like this tutorial, please check out my other tutorials by clicking ==> H3R3


* - 6e756c6c - May 3, 2010

Er....why is it + 0.5 ?


* - pr0t0typ3 - May 3, 2010

(May 3, 2010 10:10 PM)Aeonhack Wrote:  Er....why is it + 0.5 ?

I have no idea but I think it is wrong because the returned value will be 0.5 too big


* - Fake. - May 3, 2010

Use a double not integer to be precise..
Code
CDbl(CurrVal / MaxVal * 100)

And have your inputs doubles too.


* - 6e756c6c - May 4, 2010

This code should be...

Code
Public Function Percent(ByVal current As Double, ByVal maximum As Double) As Integer
        Return current / maximum * 100
    End Function



* - codepoet - May 4, 2010

all fixed to your preference


* - Learn4Life - May 4, 2010

There should not be the +0.5 ... anyway in math no.


* - codepoet - May 4, 2010

@ Learn4Life....im sorry i can't understand your question.