Number rounding in JavaScript

By: sujal
Published: August 6th, 2008

So trivial a task to us humans, almost so to JavaScript as well. Round up your brain cells as we explore number rounding in the language!

Basic idea

JavaScript helps lay the foundation for rounding off numbers with the following method:

Math.round(x)

Using it, any supplied argument is rounded off to the nearest integer, and using the “.5″ up rule. For example:

Math.round(25.9) //returns 26
Math.round(25.2) //returns 25
Math.round(-2.58) //returns -3

If purging numbers of decimals is all that you require, class dismissed!

Taking things a few decimals further

Want to display $25 in standard currency format? How about PI to finity and not beyond? Formatting numbers to specific decimal points entails still Math.round(), but padded with a little multiplication and division. See if you can identify the magic formula involved, with the below examples:

var original=28.453
1) //round "original" to two decimals
var result=Math.round(original*100)/100  //returns 28.45
2) // round "original" to 1 decimal
var result=Math.round(original*10)/10  //returns 28.5
3) //round 8.111111 to 3 decimals
var result=Math.round(8.111111*1000)/1000  //returns 8.111

In case you haven’t picked up on it, the formula to round any number to x decimal points is:

1) Multiple the original number by 10^x (10 to the power of x)
2) Apply Math.round() to the result
3) Divide result by 10^x

Ahh, pre Algebra class all over again…

Convert - C# to VB.NET or VB.NET to C#

By: sujal
Published: July 1st, 2008

Simply paste in your C# code and this free utility will automatically convert it to its equivalent in VB.NET.

Click here to convert C# to VB.NET

Simply paste in your VB.NET code and this free utility will automatically convert it to its equivalent in C#.

Click here to convert VB.NET to C#

Thanks to developerfusion.co.uk for making this utility.

How to Fade a Form using Windows Forms 2.0

By: admin
Published: June 28th, 2008

There have been plenty of articles written on fading out a form. However I found them to contain a lot of code to do this simple thing. In this article, I will demonstrate how to fade out a form with just 5 lines of code.

Try it here

Thanks to DotNetCurry Team.

C#

By: AnkitaS
Published: June 24th, 2008

Please let me know some good ebooks for C# and do temme some gud sites through which i can learn C# online…. Like some C# tutorial for some particular duration.

Thank You !!!

i wanted help on reading rs232 port

By: avinashar
Published: May 20th, 2008

i m devaloping a program to interface CNC mechines in system for which the program has to read the data from serial port and i want it read also with the running screen on CNC mechine can anyone help me over it

Recent Entries

Recent Comments

Social Network