Questions tagged [rounding]

Rounding a numerical value means replacing it by another value that is approximately equal but has a shorter, simpler, or more explicit representation.

Rounding a numerical value means replacing it by another value that is approximately equal but has a shorter, simpler, or more explicit representation; for example, replacing £23.4476 with £23.45, or the fraction 312/937 with 1/3, or the expression √2 with 1.414.

5484 questions
4063
votes
91 answers

How to round to at most 2 decimal places, if necessary

I'd like to round at most two 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?
stinkycheeseman
  • 43,437
  • 7
  • 30
  • 49
2314
votes
36 answers

Limiting floats to two decimal points

I want a to be rounded to 13.95. I tried using round, but I get: >>> a 13.949999999999999 >>> round(a, 2) 13.949999999999999 For the analogous issue with the standard library Decimal class, see How can I format a decimal to always show 2 decimal…
kevin
1458
votes
39 answers

How to round a number to n decimal places in Java

What I would 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 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
775
votes
30 answers

How do you round UP a number?

How does one round a number UP in Python? I tried round(number) but it rounds the number down. Here is an example: round(2.3) = 2.0 and not 3, as I would like. Then I tried int(number + .5) but it round the number down again! Example: int(2.3 +…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
768
votes
24 answers

Show a number to two decimal places

What's the correct way to round a PHP string to two decimal places? $number = "520"; // It's a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output should be 520.00; How should the round_to_2dp()…
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
643
votes
32 answers

Formatting a number with exactly two decimals in JavaScript

I have this line of code which rounds my numbers to two decimal places. But I get numbers like this: 10.8, 2.4, etc. These are not my idea of two decimal places so how I can improve the following? Math.round(price*Math.pow(10,2))/Math.pow(10,2); I…
Abs
  • 56,052
  • 101
  • 275
  • 409
616
votes
21 answers

How to round to 2 decimals with Python?

I am getting a lot of decimals in the output of this code (Fahrenheit to Celsius converter). My code currently looks like this: def main(): printC(formeln(typeHere())) def typeHere(): global Fahrenheit try: Fahrenheit =…
Dolcens
  • 6,201
  • 2
  • 12
  • 12
598
votes
25 answers

How do you round to one decimal place in JavaScript?

Can you round a number in JavaScript to one character after the decimal point (properly rounded)? I tried the *10, round, /10, but it leaves two decimals at the end of the int.
Walker
  • 128,495
  • 27
  • 68
  • 94
593
votes
13 answers

Round a double to 2 decimal places

If the value is 200.3456, it should be formatted to 200.34. If it is 200, then it should be 200.00.
Rajesh
  • 8,477
  • 5
  • 20
  • 7
590
votes
5 answers

Why does Math.round(0.49999999999999994) return 1?

In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5. for (int i = 10; i >= 0; i--) { long l = Double.doubleToLongBits(i + 0.5); double x; do { x =…
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
486
votes
16 answers

How do you round a number to two decimal places in C#?

I want to do this using the Math.Round function
Laila
478
votes
15 answers

Why does Math.Round(2.5) return 2 instead of 3?

In C#, the result of Math.Round(2.5) is 2. It is supposed to be 3, isn't it? Why is it 2 instead in C#?
jeffu
  • 4,897
  • 2
  • 19
  • 6
408
votes
19 answers

How to round float numbers in javascript?

I need to round for example 6.688689 to 6.7, but it always shows me 7. My method: Math.round(6.688689); //or Math.round(6.688689, 1); //or Math.round(6.688689, 2); But result always is the same 7... What am I doing wrong?
Vitalii Ponomar
  • 10,686
  • 20
  • 60
  • 88
363
votes
15 answers

Formatting Decimal places in R

I have a number, for example 1.128347132904321674821 that I would like to show as only two decimal places when output to screen (or written to a file). How does one do that? x <- 1.128347132904321674821 EDIT: The use of: options(digits=2) Has…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
354
votes
32 answers

Get decimal portion of a number with JavaScript

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 portion is easy: n = Math.floor(n); But I am…
Oscar
  • 3,551
  • 2
  • 15
  • 5
1
2 3
99 100