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?

```
var discount = (price / listprice).toFixed(2);
```

toFixed will round up or down for you depending on the values beyond 2 decimals.

Example: http://jsfiddle.net/calder12/tv9HY/

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

try using `discount.toFixed(2);`

To get the result with two decimals, you can do like this :

```
var discount = Math.round((100 - (price / listprice) * 100) * 100) / 100;
```

The value to be rounded is multiplied by 100 to keep the first two digits, then we divide by 100 to get the actual result.

If you use `+discount.toFixed(2)`

you will get a Number rather than a string.

A small variation on the accepted answer. `toFixed(2)`

returns a string, and you will always get two decimal places. These might be zeros. If you would like to suppress final zero(s), simply do this:

```
var discount = + ((price / listprice).toFixed(2));
```

There's some limitations with Math.round and .toFixed you should be aware of when dealing with two decimals. If you try to round 1.005 then you'll get the result of 1, instead of 1.01 using toFixed(2) or Math.round(1.005 * 100) / 100.

You can use following to solve this issue:

```
Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2');
```

You could make a function that will handle the rounding for you:

```
function round(value, decimals) {
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}
```

Example: https://jsfiddle.net/k5tpq3pd/1/

You could override Math.round() function and add a second parameter that defines the number of decimals. Keep in mind that this is changing a buildt in javascript function and what consequences that may have.

```
var round = Math.round;
Math.round = function (value, decimals) {
decimals = decimals || 0;
return Number(round(value + 'e' + decimals) + 'e-' + decimals);
}
```

Ande use it like this: Math.round(Number, Decimals):

```
Math.round(100 - (price / listprice) * 100, 2));
```

Example https://jsfiddle.net/k5tpq3pd/2/

Source: http://www.jacklmoore.com/notes/rounding-in-javascript/

**Fastest Way** - faster than toFixed():

```
x = .123456
result = Math.round(x * 100) / 100 // result .12
```

```
x = .123456
result = Math.round(x * 1000) / 1000 // result .123
```

Here is a working example

```
var value=200.2365455;
result=Math.round(value*100)/100 //result will be 200.24
```

To handle rounding to any number of decimal places, a function with 2 lines of code will suffice for most needs. Here's some sample code to play with.

```
var testNum = 134.9567654;
var decPl = 2;
var testRes = roundDec(testNum,decPl);
alert (testNum + ' rounded to ' + decPl + ' decimal places is ' + testRes);
function roundDec(nbr,dec_places){
var mult = Math.pow(10,dec_places);
return Math.round(nbr * mult) / mult;
}
```

```
function discoverOriginalPrice(discountedPrice, salePercentage) {
var originalPrice = discountedPrice / (1 - (salePercentage * .01));
return +originalPrice.toFixed(2);
}
```

Similar Questions

How do you make it so that all calculations in the DB compute to a pre-specified # of decimal places? Say I have three tables with the following fields Table1 A int B decimal(18, 3) Table2 A int B

I have a double value as 22.368511 I want to round it to 2 decimal places. i.e. it should return 22.37 How can I do that?

Is there a native PHP function which tells me how many decimal places a number has, or will I have to make my own? IE: 0.8 -> 1 0.2345 -> 4 0.894 -> 3

When displaying the value of a decimal currently with .ToString(), it's accurate to like 15 decimal places, and since I'm using it to represent dollars and cents, I only want the output to be 2 decima

I want to round up a number to 2 decimal places, such as 312.12. Here is my code which doesn't work correctly. Could you please suggest what I am doing wrong. (document.getElementById(AmtReturn).val

I'm new to programming and I am starting with javascritpt, I have this code and I want the printed values to be to 3 decimal places. <script> for (var a = 3, b = 2; b >= 0; a ++, b --) { docu

From what I have read, a value of data type double has an approximate precision of 15 decimal places. However, when use an irrational number, such as 1.0/7.0, I find that the variable holds the value

How to retain two decimal points in flex using Math.Round code: txtTotal.text=Math.round((Number(txtRate.text)*Number(txtQuantity.text))).toString(); here the rate is a constant 1.1 and quantity is v

I need to convert a float number into 2 decimal format.I have tried Math.round(AmountSpent1,2) This is my code: float AmountSpent1 = float.Parse(FixedAmount); Math.Round(AmountSpent1, 2);

I am converting my functionalities from the normal class library to PCL. In the normal c# class library we can pass three parameters to the math.Round function to round a decimal number. Previously we

I'm trying to work out how to round a decimal to .49 or .99. I have found the toFixed(2) function, but not sure how to round up or down. Basically need to get to the closest price point, so for exampl

I have a table (say table1) with one field of data type float (say field1). The values in this field are all unique with upto 8 decimal places. I executed following queries on this table: SELECT COUN

Is there an easy way or integrated function to find out the decimal places of a floating point number? The number is parsed from a string, so one way is to count the digits after the '.' sign, but tha

I need to round off the decimal value to decimal places using javascript. Ex,: 16.181 to 16.18 16.184 to 16.18 16.185 to 16.19 16.187 to 16.19 I have found some answers, but most of them do not roun

How do I round a double to 5 decimal places, without using DecimalFormat?

I'm using python 2.75 and my job is to print out a receipt of my cafe but I don't know how to print out my prices in currency value to 2 decimal places as it'll just print out to 1 decimal places. I'm

I am trying to create a loan calculator that will give me the calculated monthly payment in a number with a maximum of two decimal places. I am currently using the following formula, but it gives me m

This question already has an answer here: Format number to always show 2 decimal places 11 answers I'm a javascript-writing novice... Ben posted this on javascript file upload size validation t

I was having trouble displaying some prices on my website in currency format (2 decimal places). Instead the values were being displayed at 4 decimal places. Someone edited my original question and ma

What is the javaScript Math.Floor() equivalent in VBA?. this function should remove all the decimal places and return only an integer.

Possible Duplicate: round double to two decimal places in java I want to round up the double value upto 2 decimal points. for example: I have double d=2; and the result should be result =2.00

The System.printf(%.2f, currentBalance) is working fine but the problem is the rounded number appears after the sentence. Put the code into your eclipse program and run it and you can see something

I have a very strange problem which is occurring on only one pc out of my many customers. I have tracked it down to the the Math.Round method. On this brand new win7 pc Math.Round(1.59999,2) returns

i am working on a problem to round decimal values everything is fine but at one certain point it gives me error input string was not formatted after debugging my code i found 4.90702817E-05 this which

Do not quite understand the jquery but in spite of that I have created the following code, but the field IGV dollar does not throw me two decimal places. For example if I enter 1200 gives me a value o

I am trying to extract the difference between two SQL DateTime values in seconds, with decimal places for some performance monitoring. I have a table, Pagelog which has a created and end dateti

How do I get an input field (text) to accept only numbers and to automatically format it to two decimal places, as the user is typing in each digit? For example, the default value of the field is 0.00

I have a range containing numbers with different numbers of decimal places. I need to write a code which formats the whole range such that the number of decimal places is equal to the maximum number

I've tried various things and nothing is working out quite like it should. I've used the office button, excel options, advanced, and set the automatic insert decimal place to 0'. Then I tried the form

Fixed decimal places is easy String.Format({0:F1}, 654.321); gives 654.3 How do I feed the number of decimal places in as a parameter like you can in C? So String.Format({0:F?}, 654.321, 2); g

I have a decimal in .NET and I need to store it to a column in a typed dataset that is a string. I need the decimal to be stored to two decimal places. Even though I use Math.Round(value,2) and I do a

I have a question about formatting my axis on highcharts. I want to display the ticks on the Y axis in decimals with 2 decimal places. The current situation: 0 0.05 0.1 0.15 0.2 I want to display my

How do I get the javascript and php to display two decimal places after calculation. Right now it works fine but no decimal places, just whole numbers php******** <td> width=3><input t

var1=anyInteger var2=anyInteger (Math.round(var1/var2)*var2) What would be the syntax for JavaScripts bitshift alternative for the above? Using integer not floating Thank you

Possible Duplicate: c# - How do I round a decimal value to 2 decimal places (for output on a page) How to round decimal value up to nearest 0.05 value??, the linked SO post also discusses the simila

I want any items that are rounded such as this: (5.101 * 100).round / 100.0 To be output like this: 5.10 Instead of this: 5.1 How do I do this in Ruby?

This question already has an answer here: Round a double to 2 decimal places 14 answers I have a short equation: double compute,computed2; compute=getminutes/60; where getminutes is int and I

I'm not even sure I asked the question in a way that is accurately describing my issue. In JavaScript, I would like to round a given number(x) up based on another decimal(y). For example: x = 8.6333 y

Which datatype should I be using to give me a number with 2 decimal places? I currently have the following line: command.Parameters.Add(@miles, SqlDbType.Decimal, 2).Value = miles; and I am not sur

Math.Round(8.075, 2, MidpointRounding.AwayFromZero) returns 8.07, though it should return 8.08. Mysteriously enough, 7.075 is working fine, but 9.075 also returns 9.07! What to do? Does anybody know a

Am having edittext box which have to allow user to enter upto 7 numbers and two decimal places. After entering seven digits,it should not allow to add one more digit but i may allow upto 2 decimal pla

Possible Duplicate: Does setprecision in c++ round? If so why am I seeing this? Here is the function I'm using char* round(double value, int decimal_places) { decimal_places = decimal_places+1; std:

This question already has an answer here: Variable interpolation inside printf-style formatting functions 2 answers In Objective-C, how do you round a number to n decimal places, when n is a va

I have a value like 1724.1000000000001,How can I round up values like such to 1724.11 in javascript? I need a generic function.Please help...

This question already has an answer here: Round just decimal in Javascript 4 answers Javascript rounding to two decimal places 3 answers I have a javacript output 14.85 now i want to make i

I have a basic question. I'm not really good at programming. I'm trying to display an answer to 2 decimal places in my answer field. I have my JTextFields and my JRadioBtn set. When the answer comes f

I have this : i=4.568; document.write(i.toFixed(2)); output : 4.57 But i don't want to round the last number to 7 , what can i do?

When calculating a golf handicap differential you are supposed to truncate the answer to 1 decimal place without rounding. No idea why but... I know how to do this using TRUNCATE() in mySQL SELECT T

What I'd like is a method to convert a double to a string which rounds using the half-up method. I.e. if the decimal to be rounded is a 5, it always rounds up the previous number. This is the standard

Possible Duplicate: Rails 3. How do display two decimal places in edit form? I know I can use sprintf to limit to two decimal places. When I redisplay the prices in a form or just in a table, somet