Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 11:00 AM
nathan
Guest
 
Posts: n/a
Default Dividing variables and rounding

Hey folks, I'm doing up a stats page for a survey, and this is a small
part of one of the results. Below is part of the code to do
totals/percents. My question is, how do i make it so myAnswer is
written with two decimal places?

Currently, it displays the percent as 4.76190476190476

<%
myAnswer = OneAgree_total / OneTotal_total * 100
Response.Write "" & myAnswer
%>

Thanks folks.
  #2  
Old July 19th, 2005, 11:00 AM
Harag
Guest
 
Posts: n/a
Default Re: Dividing variables and rounding


Look up the "FormatNumber" function

myAnswer = FormatNumber(OneAgree_total / OneTotal_total * 100 ,2)

or you could *100 then make it an integer/long then devide by 100

eg:

My answer = clng((oneAgree_Total/ OneTotal_total)*100) / 100



HTH.
Al.

On 9 Jan 2004 05:48:59 -0800, n4th4n@hushmail.com (nathan) wrote:
[color=blue]
>Hey folks, I'm doing up a stats page for a survey, and this is a small
>part of one of the results. Below is part of the code to do
>totals/percents. My question is, how do i make it so myAnswer is
>written with two decimal places?
>
>Currently, it displays the percent as 4.76190476190476
>
><%
>myAnswer = OneAgree_total / OneTotal_total * 100
>Response.Write "" & myAnswer
>%>
>
>Thanks folks.[/color]

  #3  
Old July 19th, 2005, 11:01 AM
dlbjr
Guest
 
Posts: n/a
Default Re: Dividing variables and rounding

<SCRIPT language="vbscript" runat="server">
'Place these function in an include
'file to use on all pages

'Validate a Number
function CheckNumber(strData)
strData = Trim(strData)
If IsNUmeric(strData) Then
CheckNumber = strData
Else
CheckNumber = 0
End If
End Function

'Trap Divide By 0 Errors
function Divide(strNom,strDeNom)
strNom = CheckNumber(strNom)
strDeNom = CheckNumber(strDeNom)
If CDbl(strNom) > 0 And CDbl(strDeNom) > 0 Then
Divide = strNom / strDeNom
Else
Divide = 0
End If
End Function
</SCRIPT>


<%
'Here is the asp code
myAnswer = FormatNumber(Divide(OneAgree_total,OneTotal_total) * 100,2)
Response.Write myAnswer
%>


-dlbjr

Discerning resolutions for the alms


 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles