Home > InDesign Scripts > Rounding numbers in JavaScript

Rounding numbers in JavaScript

Very often we need to round some numbers, and Javascript has function to round numbers, but sometimes it’s not working very good. Luckily we can round number through math functions. First, let’s see how to use built-in Javascript functions. This script was also published on Adobe’s InDesign scripting forum. I collected some more info’s and combined them all into this post.

First we declare our test number:

var myNum = Number(12.248481545454);

1. Rounding to Integer

Built in Javascript function for rounding numbers to integers:

var myRounded = Math.round(myNum); // returns 12

round() – Returns the value of a number rounded to the nearest integer

var myRounded = Math.ceil(myNum); // returns 13

ceil() – Returns the smallest integer greater than or equal to the number

var myRounded = Math.floor(myNum); // returns 12

floor() – Returns the largest integer less than or equal to a number

Another way to get ‘floor’ of a number is to add two tildas before number variable like this (thanks Marc!):

var myRounded = (~~myNum); // returns 12

2. Rounding with decimals
Built in Javascript function for rounding decimal numbers:

var myRounded = myNum.toFixed(2); // returns 12.25

toFixed() – formats a number to use a specified number of trailing decimals

We can also do it with round, but we need to use math. It looks something like this (two decimal rounding):

var myRounded = Math.round(myNum*100)/100; // returns 12.25

This is safest way for rounding numbers. We can write short function for rounding, but what I do, is to create prototype for Number object with ability to input number of decimals I want. This function looks like this:

Number.prototype.doRound = function(roundNum, roundDec){
	var roundMulit = Math.pow(10,roundDec);
	return Math.round(roundNum*roundMulit)/roundMulit;
}

We now can pass number we want to round, and number of decimals, and we get nice rounded number. Let’s see how to use this:

With two decimals:

var myRounded = myNum.doRound(myNum,2); // returns 12.25

With three decimals:

var myRounded = myNum.doRound(myNum,3); // returns 12.248

That’s it, nice, clean and short! Have fun!😀

[UPDATE]

There is one little mistake in prototype code. Instead of passing number we want to round again, we can use ‘this’ object to pass value. Here is corrected code:

Number.prototype.doRound = function(roundDec){
	var roundMulit = Math.pow(10,roundDec);
	return Math.round(this*roundMulit)/roundMulit;
}

And here is usage example:

var myRounded = myNum.doRound(2); // returns 12.25

With three decimals:

var myRounded = myNum.doRound(3); // returns 12.248
Categories: InDesign Scripts Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: