Questions tagged [double]

Double is a primitive data type used to store fractional numbers that holds a double-precision floating-point (often 64 bits).

In C and C++ double must be at least as large as float (another floating-point type), but its actual size can vary from system to system as it is implementation-defined. It is typically twice the size of float. Many systems store doubles in binary64, a format described in the IEEE 754 technical standard.

Java has a very strict definition for double, requiring it to be stored in binary64 (which is also called double-precision).

More information:

7762 questions
132
votes
14 answers

Scala Doubles, and Precision

Is there a function that can truncate or round a Double? At one point in my code I would like a number like: 1.23456789 to be rounded to 1.23
richsoni
  • 3,908
  • 8
  • 30
  • 43
131
votes
12 answers

Is it possible to get 0 by subtracting two unequal floating point numbers?

Is it possible to get division by 0 (or infinity) in the following example? public double calculation(double a, double b) { if (a == b) { return 0; } else { return 2 / (a - b); } } In normal cases it…
Thirler
  • 18,868
  • 13
  • 58
  • 86
125
votes
14 answers

Double decimal formatting in Java

I'm having some problems formatting the decimals of a double. If I have a double value, e.g. 4.0, how do I format the decimals so that it's 4.00 instead?
Christoffer
  • 1,449
  • 2
  • 11
  • 16
123
votes
8 answers

Rounding a double to turn it into an int (java)

Right now I'm trying this: int a = round(n); where n is a double but it's not working. What am I doing wrong?
David
  • 13,509
  • 33
  • 71
  • 101
121
votes
3 answers

Converting double to integer in Java

In Java, I want to convert a double to an integer, I know if you do this: double x = 1.5; int y = (int)x; you get y=1. If you do this: int y = (int)Math.round(x); You'll likely get 2. However, I am wondering: since double representations of…
vdMandele
  • 1,409
  • 2
  • 9
  • 8
119
votes
14 answers

Swift double to string

Before I updated xCode 6, I had no problems casting a double to a string but now it gives me an error var a: Double = 1.5 var b: String = String(a) It gives me the error message "double is not convertible to string". Is there any other way to do…
tim_yng
  • 2,231
  • 4
  • 14
  • 19
119
votes
5 answers

Converting a double to an int in C#

In our code we have a double that we need to convert to an int. double score = 8.6; int i1 = Convert.ToInt32(score); int i2 = (int)score; Can anyone explain me why i1 != i2? The result that I get is that: i1 = 9 and i2 = 8.
Wouter Dorgelo
  • 10,988
  • 11
  • 55
  • 75
118
votes
10 answers

Why does dividing two int not yield the right value when assigned to double?

How come that in the following snippet int a = 7; int b = 3; double c = 0; c = a / b; c ends up having the value 2, rather than 2.3333, as one would expect. If a and b are doubles, the answer does turn to 2.333. But surely because c already is a…
Jahoe
  • 1,476
  • 2
  • 11
  • 27
117
votes
9 answers

MySQL pagination without double-querying?

I was wondering if there was a way to get the number of results from a MySQL query, and at the same time limit the results. The way pagination works (as I understand it), first I do something like query = SELECT COUNT(*) FROM `table` WHERE…
atp
  • 27,422
  • 41
  • 120
  • 179
111
votes
7 answers

Force point (".") as decimal separator in java

I currently use the following code to print a double: return String.format("%.2f", someDouble); This works well, except that Java uses my Locale's decimal separator (a comma) while I would like to use a point. Is there an easy way to do this?
dtech
  • 12,458
  • 11
  • 42
  • 67
111
votes
20 answers

How to get the Power of some Integer in Swift language?

I'm learning swift recently, but I have a basic problem that can't find an answer I want to get something like var a:Int = 3 var b:Int = 3 println( pow(a,b) ) // 27 but the pow function can work with double number only, it doesn't work with…
林鼎棋
  • 1,685
  • 2
  • 12
  • 23
108
votes
16 answers

How can I truncate a double to only two decimal places in Java?

For example I have the variable 3.545555555, which I would want to truncate to just 3.54.
Johnny
  • 1,239
  • 3
  • 12
  • 13
108
votes
5 answers

Dealing with float precision in Javascript

I have a large amount of numeric values y in javascript. I want to group them by rounding them down to the nearest multiple of x and convert the result to a string. How do I get around the annoying floating point precision? For example: 0.2 +…
Jeroen
  • 28,942
  • 33
  • 116
  • 190
107
votes
6 answers

How to print a double with two decimals in Android?

Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem: I have a var: double a; And I want to show it only with 2 or 3…
ArcDare
  • 2,926
  • 4
  • 25
  • 38
106
votes
10 answers

Convert float to double without losing precision

I have a primitive float and I need as a primitive double. Simply casting the float to double gives me weird extra precision. For example: float temp = 14009.35F; System.out.println(Float.toString(temp)); // Prints…
Steve Armstrong
  • 4,696
  • 5
  • 30
  • 42