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:

8301 questions
143
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,629
  • 2
  • 11
  • 8
143
votes
15 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
  • 4,188
  • 8
  • 34
  • 47
141
votes
16 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,591
  • 4
  • 18
  • 20
141
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,995
  • 2
  • 16
  • 25
139
votes
3 answers

Why does this random value have a 25/75 distribution instead of 50/50?

Edit: So basically what I'm trying to write is a 1 bit hash for double. I want to map a double to true or false with a 50/50 chance. For that I wrote code that picks some random numbers (just as an example, I want to use this on data with…
gvlasov
  • 18,638
  • 21
  • 74
  • 110
136
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
  • 14,569
  • 34
  • 78
  • 107
135
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,666
  • 2
  • 12
  • 27
133
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 +…
132
votes
13 answers

Round up double to 2 decimal places

How do I round up currentRatio to two decimal places? let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)! railRatioLabelField.text! = "\(currentRatio)"
Del Hinds
  • 2,887
  • 4
  • 12
  • 12
132
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
  • 20,239
  • 14
  • 63
  • 92
132
votes
13 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,527
  • 2
  • 11
  • 16
131
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) is to first do something like: query = SELECT COUNT(*) FROM `table` WHERE…
atp
  • 30,132
  • 47
  • 125
  • 187
127
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
  • 13,741
  • 11
  • 48
  • 73
125
votes
18 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,419
  • 3
  • 13
  • 13
121
votes
11 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
  • 5,252
  • 7
  • 32
  • 43