# 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

- changing value of threshold value when getting error…
- Getting "requires numeric/complex matrix/vector…
- Is CSS Turing complete?
- Dynamically update values of a chartjs chart
- How can I format a decimal to always show 2 decimal places?
- 3D Rotation of a camera using its own, new axes
- Python: Float to Decimal conversion and subsequent…
- What does this symbol mean in JavaScript?
- Round number to nearest integer
- Siemens LOGO! PLC data in the wrong order
- Why the value of input file missing when I input the…
- How to truncate float values?
- Assigning a variable NaN in python without numpy
- What is a lambda expression in C++11?
- Align input points with multiple variable lengths
- Round a double to 2 decimal places
- Design DFA accepting binary strings divisible by a…
- How can I compare values between two data frame…
- Polymer paper-input doesn't seem to properly block…
- python pandas remove duplicate columns
- Display number always with 2 decimal places in
- How to round the double value to 2 decimal points?
- DQN Pytorch Loss keeps increasing
- Sort table rows In Bootstrap
- Is floating point math broken?
- Regular expression to match numbers with or without…
- Formatting a number with exactly two decimals in JavaScript
- Rounding a variable to two decimal places C#
- How do you calculate log base 2 in Java for integers?
- Limiting double to 3 decimal places
- Java Currency Number format
- Python stack loses data
- How do I convert a float number to a whole number in…
- Excel CSV - Number cell format
- How to implement a basic iterative pushdown…
- I have these 2 tonnage calculators. The first one is…
- How do you round to 1 decimal place in Javascript?
- Reference - What does this regex mean?
- Simplest way to create Unix-like continuous pipeline…
- Verilog: How to instantiate a module
- How do you rotate canvas element to mouse position,…
- Convert array to nested JSON object - Angular Material tree
- Polymer focus() has to be wrapped in setTimeout() to work?
- How to detect page zoom level in all modern browsers?
- Firebase cloud function onUpdate is triggered but…
- How to make Javascript font responsive?
- React google map is not updating
- Meaning of delta or epsilon argument of assertEquals…
- Validate that a string is a positive integer
- Generating random whole numbers in JavaScript in a…
- VueJS: Why Trigger 'Input' Event Within 'Input'…
- How do I limit the number of digits from 6 to 4 in…
- Difference between numeric, float and decimal in SQL Server
- JavaScript math, round to two decimal places
- Vue js vuecli3 application does not work in ie11…
- Get the decimal part from a double
- Printf width specifier to maintain precision of…
- Calculating distance between two points, using…
- Active tab issue on page load HTML
- How to add a legend entry for an errorbar in a barchart?
- Reorganize number order
- IsNumeric gives unexpected results in excel/vba for…
- In unity how to acces transform rotation in script
- Changing plot scale by a factor in matplotlib
- What are the undocumented features and limitations…
- How can I remove the decimal part from JavaScript number?
- VueJS watching properties and changing data
- Difference between decimal, float and double in .NET?
- How to determine if a decimal/double is an integer?
- Why does Math.Round(2.5) return 2 instead of 3?
- How to define a group of class pairs that have a…
- Why in some case the selected of the iron-page doesn't work?
- Javascript: formatting a rounded number to N decimals
- How to round a floating point number up to a certain…
- Why the incorrect toggle action in Polymer core-list…
- unsupported operand type(s) for *: 'NoneType' and…
- Svelte - using select inputs with an on:change event
- Aurelia validate one datetime is greater than the other
- How to round an average to 2 decimal places in PostgreSQL?
- Begin, Rescue and Ensure in Ruby?
- Round double in two decimal places in C#?
- How to match string between two words, and repeat…
- Formatting Decimal places in R
- I need to round a float to two decimal places in Java
- How to Round to the nearest whole number in C#
- Correct way to convert size in bytes to KB, MB, GB…
- Converting double to integer in Java
- Python 3 Float Decimal Points/Precision
- Backbone.Marionette - Collection within…
- Converting between color models
- round() for float in C++
- Select SQL Server database size
- Understanding constexpr functions with templates [C++]
- How to use aurelia-validate with a object properties…
- numeric input component for vue js
- How can I read inputs as numbers?
- Evaluating a mathematical expression in a string
- How to round a Double to the nearest Int in swift?
- Programmatically Lighten or Darken a hex color (or…
- Issue Instantiating Block in Minecraft like Game

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