On the typical PC, Java Script Math object methods can return floating point numbers with up to 16 digits to the right of the decimal point. For most purposes, this kind of accuracy is not required, nor desired, and that many digits causes nothing but confusion. In this article, You'll learn how to round a floating point number to 4 digits to the right of the decimal point.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact Advertise on Bucaro TecHelp Advertise Here RSS News Feeds News Feeds

HTML5 Solutions: Essential Techniques for HTML5 Developers

Essential Techniques for HTML5 Developers

HTML5 brings the biggest changes to HTML in years. Web designers now have new techniques, from displaying video and audio natively in HTML, to creating realtime graphics on a web page without a plugin.

This book provides a collection of solutions to all of the most common HTML5 problems. Every solution contains sample code that is production-ready and can be applied to any project.

Click Here

Easy Java Script Code to Round a Float to 4 Digits to the Right of the Decimal Point

Float:

On the typical PC, Java Script Math object methods can return floating point numbers with up to 16 digits to the right of the decimal point. For most purposes, this kind of accuracy is not required, nor desired, and that many digits causes nothing but confusion. In this article, You'll learn how to round a floating point number to 4 digits to the right of the decimal point.

For this example, we'll input test numbers through a form. The code for the form is shown below.

<form>
Float: <input style="text-align:right" type="text" name="floatNum">
<input type="button" value="Round" onClick="roundNum(this.form)">
</form>

Note that the onclick event of the form's [Round] button sends the form's data to a function named roundNum. Shown below is the shell of the roundNum function. paste this Java Script code block into the <head> section of your web page.

<script type="text/javascript">
function roundNum(form)
{
  var strFloat = form.floatNum.value;
  var newFloat;
}
</script>

Note that two variables are declared at the top of the function. The variable strFloat receives the value entered into the form's text box. The variable newFloat will be used to hold the output float rounded to 4 digits to the right of the decimal point.

Just below the varaiable declarations, we add an if structure to test if there are more than 4 digits to the right of the decimal point in the input number. If there are, make a substring with 5 digits to the right of the decimal point. If not (else), you can just set the output variable, newFloat, to the same value as the input variable.

function roundNum(form)
{
  var strFloat = form.floatNum.value;
  var newFloat;

  if(strFloat.length - strFloat.indexOf(".") > 5)
  {
    strFloat = strFloat.substring(0,strFloat.indexOf(".") + 6);
  }
  else
  {
    newFloat = floatNum;
  }
}

RSS Feed RSS Feed



Web Design Sections

Eloquent JavaScript: A Modern Introduction to Programming

Eloquent JavaScript

Eloquent JavaScript Eloquent JavaScript goes beyond the cut-and-paste scripts of the recipe books and teaches you to write code that's elegant and effective. You'll start with the basics of programming, and learn to use variables, control structures, functions, and data structures. Then you'll dive into the real JavaScript artistry: higher-order functions, closures, and object-oriented programming.

Reader Anthony says, "This book is not your typical Javascript book. Others have a utilitarian approach. In stark contrast, Eloquent JavaScript does not merely provide you a checklist of things to learn but rather paints a panorama of the possibilities that programming provides. Javascript is merely the tool used to introduce these to the reader.

Click here for more information.


[Site User Agreement] [Advertise on This site] [Search This Site] [Contact Form]
Copyright©2001-2011 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268