Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Precent


Reply
 
Thread Tools Display Modes
  #1  
Old 12-19-2001, 09:42 AM
zapzapper
Guest
 
Posts: n/a
Question Precent


I need help on putting a code into a program that firgures precent I had one but everytime that I ran it and a zero was in the program it would stop I know that you can not divide by zero but if a zero get into the program by mistake it will not run I need a if statement to get around this so that if a zero get into it then in the total box it will still say 0% and keep on running I am a newbie at this
thanks
Roger
Reply With Quote
  #2  
Old 12-19-2001, 09:50 AM
dcl3500's Avatar
dcl3500 dcl3500 is offline
Mostly Harmless?

Retired Moderator
* Expert *
 
Join Date: Jun 2001
Location: Western Illinois, USA
Posts: 2,398
Default Re: Precent

Try this on a form with text1, text2 and Command1

Private Function DoPercent(dInNum As Double) As Double
If dInNum = 0 Then
DoPercent = 0#
Else
DoPercent = dInNum / 100
End If
End Function

Private Sub Command1_Click()
Text2 = Format(DoPercent(Text1), "Percent")
End Sub
__________________
Don

"So long and thanks for all the fish.'" - Douglas Adams.
Reply With Quote
  #3  
Old 12-19-2001, 10:07 AM
Mill Mill is offline
Just another Excel nerd

Retired Moderator
* Guru *
 
Join Date: Feb 2000
Location: Michigan, USA
Posts: 2,624
Default Re: Precent

Actually, his problem is Divide By Zero, so you'd need something like this:

<pre>
Function CalcPercent(X as Single, Total as Single) as Single
Dim Result as Single
If Total = 0 Then
Result = 0
Else
Result = X * 100 / Total
End If

CalcPercent = Result

End Function

</pre>
__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handey
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->