I want to use Javascript to round up a number. For example like this,

```
15 => 20
345 => 400
201 => 300
180 => 200
93 => 100
22 => 30
```

How to achieve this using Javascript? I know i can use Math.ceil or Math.round but this is not decimal numbers.

Solution for your first example:

```
var n = 15;
n /= 10; //n = 1.5
n = Math.ceil(n); //n = 2
n *= 10; //n = 20
```

The other examples work the same way.

What about you divide your numbers by 10/100/1000 to have decimals, then ceil/round. Then multiply again.

This is the first solution that comes to my mind.

To round to the nearest multiple of 100, you can write

```
100*Math.ceil(x/100)
```

Similarly for other factors.

Choosing a “nice” factor might be a different problem. You could take the base-10 logarithm of your numbers, multiply that by three and round. Then you could apply the following map:

```
3*round(log10(x)) factor
0 1
1 2
2 5
3 10
4 20
5 50
```

You don't have to store that table: using modulo operations, you can compute the leading digit of the factor, and using the floor of the third part you get the number of trailing zeros.

Whether this is appropriate depends on your application. The code in your question appears to only use factors which are a power of 10. In that case, don't do the multiplication by 3 in the above approach, but instead simply use the floor of the base 10 log directly:

```
factor = Math.pow(10, Math.floor(Math.log(x)/Math.log(10)));
rounded = Math.ceil(x/factor)*factor;
```

The answer you want depends on the precision.

A basic solution would be

```
function round(num, precision) {
var div = Math.pow(10, precision);
return Math.ceil(num/div)*div;
}
```

Similar Questions

I am testing my form on MAC (Safari and Chrome) but for some reason I can't enter decimal numbers with comma. In chrome I can't have decimals i.e. 19,99 = 1999 and in safari insted of coma it uses dot

I am writing functions that serialize/deserialize a large data structure for efficient reloading later on. There is a particular set of decimal numbers for which precision is not a huge deal, and I wo

I have to store numbers with many decimal places. For example: 0.000000054 If I store it in a column float data type the value is saved as: 5.4E-8 This value has to be displayed in php template with o

I am using the following script to validate a form and would like to be able to to ensure that the inputter correctly inputs a numeric value with decimal places on the last field (amount). Can anyone

how do I use decimal numbers with bigintegers? I would suspect the way I wrote, but obv it fails: @Test public void bigIntegerTestCalcs() { BigInteger a = new BigInteger(20); BigInteger b = new BigI

This question already has an answer here: Is floating point math broken? 14 answers So I am making a script that adds two numbers (decimal numbers) together, which I have encountered a problem.

I am looking for a Regex for non decimal integer considering exponents and honestly I have tried a lot before asking here. The regex should match 1.23E4,1.2334576E34, 122E3,123,456 etc. not match 1.2

I want to avoid from decimal numbers in my Axis, how can I do that ? XAML: <Charting:Chart VerticalAlignment=Stretch HorizontalContentAlignment=Stretch> <Charting:Chart.Axes> <Cha

I am trying to print the numbers simply in the sequence i.e 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 using Loop, First i converted each number into Hexa printed it reset it to the decimal in

I'm new to C. I was writing a program to print all the decimal numbers from 0 to a given number(as provided by the user) but my output is not what I had expected. I don't understand what mistake is th

Could any of you help me with a regex to allow this: 123.123 123. 1323.2 1.2312 1,23 123123,32 123, 123412 so to sum up: only one decimal point, it can be the last one.. but most not be the first. b

I have a decimal number like this: 62.000,0000000 I need to cast that decimal into int; it will always have zero decimal numbers; so I wont loose any precision. What I want is this: 62.000 Stored on a

Possible Duplicate: Non repeating random numbers Can any one tell me how to generate non repeating random numbers? I saw this on many sites but they give in main.c file code. When I use the main.c f

I want a table containing a column of decimal numbers, of varying length before and after the decimal, with the decimal points all aligned. The column width must have a liquid size, expanding as nec

This question already has an answer here: Regular Expression to accept only positive numbers and decimals 3 answers In my phonegap app I need to validate the textbox with regular expression whi

I am using <input type=number> where I place values using Javascript. I'd like to format these numbers to have always one decimal. Chrome stubbornly strips out of trailing zero from the numbers

How can I convert words to numbers in JavaScript? Example: Nineteen days from now would become 19 days from now. I'm fine with using jQuery or another library - hopefully a smallish one if it's no

I would like to generate uniformly distributed random numbers between 0 and 0.5, but truncated to 2 decimal places. without the truncation, I know this is done by import numpy as np rs = np.random.Ran

I have xml textfiles which contain lines of multiple numbers (3) separated by tabs/spaces, from which I would like to select the each set of numbers separately. From: <tagname1> 110.0912 99.1234

The question has been answered for integers printed in decimal format, but I'm looking for an elegant way to do the same with integers in non-decimal format (like binary, octal, hex). Creation of such

I'm sure this is a simple problem, but I'm comparing negative numbers in javascript i.e.: var num1 = -83.778; var num2 = -83.356; if(num1 < num2) { // Take action 1 } else { // Take action 2 } Thi

I have a string such as 3.1 ml or abc 3.1 xywazw I'd like to extract 3.1 from this string. I have found many questions on stackoverflow about the extraction of numbers from a character string, b

I need to output decimal numbers in a price format, i.e. 10 = 10.00 11.1 = 11.10 How can I achieve this using decimal.Decimal class ? pad_zero(Decimal('10.0')) >>>Decimal('10.00') *EDIT:*for

So I need to change doubles such as 143.203030 to 143.20 rounding the second digit up or down(like money) my plan was to turn the double into an array and then if the third number after the decimal is

I have a function which I use to limit form inputs to numbers only, or number and decimals depending on the field. Allowing decimals and numbers is easy enough, but I am trying to take it a step furth

I have an annoying script I can't complete. I need 32 non-repeating numbers out of a possible 0-64 number set. Every time I try to create a loop to check the new random number against all the numbers

I have a problem with decimal numbers. If I use .(dot) instead of ,(comma) in the textbox it comes null in controller. I know its a language issue because in spanish we use comma instead of dot for de

problem is with specified input, I have an input in which is something like this, -0.982983928391353 and i want to format that to -0.98298 ( upto 5 decimal points) this is the sample input xml below.

<html xmlns=http://www.w3.org/1999/xhtml> <head> <title>Round to 2 Decimal Places</title> <script type=text/javascript src=http://ajax.microsoft.com/ajax/jQuery/jquery-

I'm using the following regexp to validate numbers in my javascript file: var valid = (val.match(/^\d+$/)); It works fine for whole numbers like 100, 200, etc, however for things like 1.44, 4.11, etc

Possible Duplicate: Large numbers erroneously rounded in Javascript I am experiencing some strange behavior when working with numbers in javascript. When I use the following code: <a href=javasc

I have a jsp with lots of javascript code. Whenever there is a javascript error on the page, shown in the status bar of the IE browser, the line number reported to contain the error, does not match wi

am generating a game using vb 6.0, at one face i need to generate non repeating random numbers between 1 and 100. am using the following code for generating the random numbers dim n(10) as integer for

In PHP textfield: Prevent the user from entering more then 3 numbers before decimal point for example: if user enter 123.12 its acceptable, if user enter 12.12 its also acceptable, but if user enter 1

There are ton of solution in Stackoverflow to calculate the totals of a column. That part I already solved. The issue is that of a total of 13 columns, only two are actually decimal numbers.This table

I was browsing through the internet to find a proper javascript regular expression for phone numbers but the ones I find are not the ones I need. The acceptable phone number type for my case would hav

Possible Duplicate: Elegant workaround for JavaScript floating point number problem Why is it that when adding 2 numbers together using javascript, it will return a crazy number of decimal points? I

I'm trying to write a Regular Expression validator. It should accept numbers only with decimal part 0 or 5 only. like, Valid Numbers 0.5 1 (1.0) 1.5 2 (2.0) 2.5 . . . etc. Invalid Numbers 0.1 1.2 1.

How do I round larger numbers in php. NOTE: I have aready tried the round function and can not seem to get it to work as i need for example: Say i have 4 listing in a database and they have 4 differen

please tell me how to generate 5 different non repeatable random number between 1,20 in javascript for unity3d this the code i have written.. this works 1st 3 times but crashes the last time. please t

I do a lot of work with numbers with two places after the decimal. My adding machine has the nice feature that I can enter a string of numbers like 123456 and it automatically inserts the decimal for

I have the below JavaScript syntax: var discount = Math.round(100 - (price / listprice) * 100); This rounds up to the whole number, how can I return the result with two decimal places?

This question already has an answer here: JavaScript: formatting number with exactly two decimals 13 answers How do you round to 1 decimal place in Javascript? 8 answers How am I to get sec

I have float numbers like 3.2 and 1.6. I need to separate the number into the integer and decimal part. For example, a value of 3.2 would be split into two numbers, i.e. 3 and 0.2 Getting the integer

I've limited the input field to only numbers through js but am not sure how to also allow decimals... function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode &g

So I am having a string that is created via function and then another function gets that string and takes only the numbers out of it. The string always looks like this (only the numbers change): rgba

Is there a method in python to generate random non repeating numbers in a range except a specific number.For example i want to generate numbers between 1 and 10 but 1 should not be generated.Any hel

How can I convert hexadecimal to decimal numbers in Emacs calc? For example, if I enter FF, I want it to convert it to 255. UPDATE: How do I get the reverse operation, turn base 10 to base 16?

There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn't have an exact binary representation, so it's dangerous to use the == ope

I'm looking for some succinct, modern C# code to generate a random decimal number between 1.41421 and 3.14159. where the number should be [0-9]{1}.[0-9]{5} format. I'm thinking some solution that util