# Round to at most 2 decimal places (only if necessary)

###### Posted By: Anonymous

I’d like to round at most 2 decimal places, but *only if necessary*.

Input:

```
10
1.7777777
9.1
```

Output:

```
10
1.78
9.1
```

How can I do this in JavaScript?

## Solution

Use Math.round() :

```
Math.round(num * 100) / 100
```

Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :

```
Math.round((num + Number.EPSILON) * 100) / 100
```

###### Answered By: Anonymous

### Related Articles

- Is CSS Turing complete?
- Dynamically update values of a chartjs chart
- How can I format a decimal to always show 2 decimal places?
- Round number to nearest integer
- Python: Float to Decimal conversion and subsequent…
- What is a lambda expression in C++11?
- How to truncate float values?
- python pandas remove duplicate columns
- What does this symbol mean in JavaScript?
- Assigning a variable NaN in python without numpy
- Why the value of input file missing when I input the…
- Round a double to 2 decimal places
- Excel CSV - Number cell format
- DQN Pytorch Loss keeps increasing
- Formatting a number with exactly two decimals in JavaScript
- Design DFA accepting binary strings divisible by a…
- Polymer paper-input doesn't seem to properly block…
- How to round the double value to 2 decimal points?
- Display number always with 2 decimal places in
- How do you calculate log base 2 in Java for integers?
- Is floating point math broken?
- Java Currency Number format
- Limiting double to 3 decimal places
- Rounding a variable to two decimal places C#
- How to detect page zoom level in all modern browsers?
- Sort table rows In Bootstrap
- Regular expression to match numbers with or without…
- Meaning of delta or epsilon argument of assertEquals…
- How do I convert a float number to a whole number in…
- Changing plot scale by a factor in matplotlib
- How do you round to 1 decimal place in Javascript?
- I have these 2 tonnage calculators. The first one is…
- How to implement a basic iterative pushdown…
- JavaScript math, round to two decimal places
- Simplest way to create Unix-like continuous pipeline…
- Difference between numeric, float and decimal in SQL Server
- How to determine if a decimal/double is an integer?
- Firebase cloud function onUpdate is triggered but…
- Generating random whole numbers in JavaScript in a…
- Vue js vuecli3 application does not work in ie11…
- Calculating distance between two points, using…
- Verilog: How to instantiate a module
- React google map is not updating
- IsNumeric gives unexpected results in excel/vba for…
- Printf width specifier to maintain precision of…
- Get the decimal part from a double
- Validate that a string is a positive integer
- What are the undocumented features and limitations…
- Difference between decimal, float and double in .NET?
- Why does Math.Round(2.5) return 2 instead of 3?
- Why the incorrect toggle action in Polymer core-list…
- How can I remove the decimal part from JavaScript number?
- Polymer focus() has to be wrapped in setTimeout() to work?
- How can I see the model as visualized?
- Javascript: formatting a rounded number to N decimals
- How to load a model from an HDF5 file in Keras?
- Aurelia validate one datetime is greater than the other
- How to round a floating point number up to a certain…
- How to round an average to 2 decimal places in PostgreSQL?
- Begin, Rescue and Ensure in Ruby?
- VueJS: Why Trigger 'Input' Event Within 'Input'…
- Deep Q Learning - Cartpole Environment
- Reference - What does this regex mean?
- JUnit assertEquals(double expected, double actual,…
- Round double in two decimal places in C#?
- Rounding numbers to 2 digits after comma
- Evaluating a mathematical expression in a string
- How can I determine whether a 2D Point is within a Polygon?
- SQL Inner-join with 3 tables?
- Svelte - using select inputs with an on:change event
- VueJS watching properties and changing data
- Python 3 Float Decimal Points/Precision
- Backbone.Marionette - Collection within…
- How to Round to the nearest whole number in C#
- round() for float in C++
- Select SQL Server database size
- I need to round a float to two decimal places in Java
- How to concatenate two layers in keras?
- Converting double to integer in Java
- Truncate Two decimal places without rounding
- Getting zeros for and or statement
- How to use aurelia-validate with a object properties…
- Formatting Decimal places in R
- TypeError: Cannot convert a symbolic Keras…
- Correct way to convert size in bytes to KB, MB, GB…
- Why in some case the selected of the iron-page doesn't work?
- How to round a Double to the nearest Int in swift?
- Convert decimal? to decimal in earlier versions of…
- Programmatically Lighten or Darken a hex color (or…
- Polymer :: Unable to target elements inside…
- Best way to get whole number part of a Decimal number
- Java adding objects according to ratios and population sizes
- Is it possible to apply CSS to half of a character?
- Extend Aurelia Validation Rules on a per class basis
- numeric input component for vue js
- Python: got an output image with unexpected grid lines
- How to calculate distance between two locations…
- Regular Expression for any number greater than 0?
- javaScript - Uncaught TypeError: Cannot read…
- String to decimal conversion: dot separation instead…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.