Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Percent Math Calculations Programming Tutorial

Learn how to calculate percentage values for things like sale pricing, mathematical positioning or sizing, and much more using JavaScript. Find out how much of a percentage one number is of another number var v1 = 160; var v2 = 40; var diffPercent = ((v2 / v1) * 100).toFixed(2); document.write(v2+" is "+diffPercent+"% of "+v1); Calculating sale prices and savings var price = 150; var sale = 25; var savings = (price * sale) / 100; var saleprice = price - savings; document.write("Original Price: $"+price.toFixed(2)+"<br>"); document.write("Sale: "+sale+"%<br>"); document.write("Sale Price: $"+saleprice.toFixed(2)+"<br>"); document.write("Savings: $"+savings.toFixed(2)+"<br>"); Positioning elements by percent according to dynamic values <style> div#bar1{ width:500px; height:40px; background:#EEE; } div#bar1 > div{ position:relative; width:3px; height:40px; background:#000; } </style> <div id="bar1"> <div></div> </div> <script> var bar1 = document.getElementById("bar1"); var percent = 50; var newleft = Math.round((bar1.offsetWidth * percent) / 100); bar1.children[0].style.left = newleft+"px"; </script> Calculate the difference as a percentage between two values var v1 = 160; var v2 = 40; var diffPercent = (((v1 - v2) / v1) * 100).toFixed(2); document.write(v2+" is "+diffPercent+"% smaller than "+v1);
thumb

JavaScript Videos

General Programming

Flat Icons HTML Code Symbols Discovery JavaScriptHow to Program Code Syntax Highlighter Using JavaScriptFat Arrow Functions JavaScript Programming TutorialJavaScript Arguments Object Function TutorialJavaScript Storage Interface sessionStorage localStorage TutorialMulti Column Layout Grid Programming TutorialCustom Alert Box Programming TutorialCustom Confirm Box Programming TutorialCustom Prompt Box Programming TutorialChange CSS Class Style className Toggle TutorialPartial Print Document Tutorial HTML div ContentClick Outside Close Menu Box TutorialPercent Math Calculations Programming TutorialDual Image Reveal Control TutorialPage Loading Screen Document Preloader TutorialControl Panel Flyout Menu Windows AnimationsCustom Data Attributes HTML JavaScript CSS TutorialFullscreen API JavaScript Code Examples and SpecificationConsole Log Tutorial Chrome Firefox IE BrowsersToggle Function Click Drop Down Menus TutorialChange Tab Text and Window Title On The Fly TutorialDice Roll Programming Tutorial For Web Browser GamesDynamic Lorem Ipsum Placeholder Text TutorialBest HTML Event Handling addEventListener TutorialsetAttribute removeAttribute Methods TutorialDisabled User Handling Redirect WebsiteCapture Keyboard Input Tutorial HTML ApplicationCapture Keyboard Input Condition Logic TutorialChange Style Sheet Using Tutorial CSS Swap StylesheetWeb Page Overlay Tutorial Transparent CoverLights Out For Earth Day Tutorial Dark Page SwitchWindow Size Responsive CSS Layout Stylesheet Change TutorialRead Mouse Coordinates Pointer Position Tutorial