Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

A divided by B displays C percent

Posted on 2007-10-15
6
Medium Priority
?
845 Views
Last Modified: 2012-05-05
Assuming you have two variables...

correctAnswersCount
questionsAnswered

How would I display to screen a number representing a percentage?  In other words, if correctAnswersCount = 12 and questionsAnswer = 24, then I want to have a number displayed like this:  50%

I also need the result round to the nearest integer.  IOW, a result of 50.7 should be displayed as 51%

0
Comment
Question by:Monroe406
  • 5
6 Comments
 
LVL 11

Expert Comment

by:Isisagate
ID: 20079616
document.getElementById("percentInput").value = (correctAnswersCount/questionsAnswered)*100;
0
 
LVL 11

Expert Comment

by:Isisagate
ID: 20079621
or

document.write((correctAnswersCount/questionsAnswered)*100);
0
 
LVL 11

Accepted Solution

by:
Isisagate earned 200 total points
ID: 20079629
document.getElementById("percentInput").value =( (correctAnswersCount/questionsAnswered)*100)+"%";

document.write(((correctAnswersCount/questionsAnswered)*100)+"%");


Forgot the percent signs
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Monroe406
ID: 20080085
Whoops!  I awarded the answer to quickly.  Your solution below fails to deal with the nearest integer part of my question.  Your answer will end up displaying unsightly decimal portions:   66.666666666666 %  instead of 67 %
0
 
LVL 11

Expert Comment

by:Isisagate
ID: 20080288
Math.round(correctAnswersCount/questionsAnswered)*100) will round up if past .5 and down if below...

Math.ceil will round up if it's larger then a whole number
Math.floor will round down if less then a whole number..


use which ever work for you
0
 
LVL 11

Expert Comment

by:Isisagate
ID: 20080292
Math.round((correctAnswersCount/questionsAnswered)*100)


forgot a ( in that snippet....
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question