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
296
votes
12 answers

When should I use double instead of decimal?

I can name three advantages to using double (or float) instead of decimal: Uses less memory. Faster because floating point math operations are natively supported by processors. Can represent a larger range of numbers. But these advantages seem to…
Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
295
votes
11 answers

Biggest integer that can be stored in a double

What is the biggest "no-floating" integer that can be stored in an IEEE 754 double type without losing precision? In other words, at would the follow code fragment return: UInt64 i = 0; Double d = 0; while (i == d) { i += 1; d +=…
Franck Freiburger
  • 26,310
  • 20
  • 70
  • 95
281
votes
19 answers

How do I parse a string with a decimal point to a double?

I want to parse a string like "3.5" to a double. However, double.Parse("3.5") yields 35 and double.Parse("3.5", System.Globalization.NumberStyles.AllowDecimalPoint) throws a FormatException. Now my computer's locale is set to German, wherein a…
Legate
263
votes
3 answers

How do I convert a string to a double in Python?

I would like to know how to convert a string containing digits to a double.
user46646
  • 153,461
  • 44
  • 78
  • 84
252
votes
9 answers

Float and double datatype in Java

The float data type is a single-precision 32-bit IEEE 754 floating point and the double data type is a double-precision 64-bit IEEE 754 floating point. What does it mean? And when should I use float instead of double or vice-versa?
Leo
  • 5,017
  • 6
  • 32
  • 55
224
votes
12 answers

Compare double to zero using epsilon

Today, I was looking through some C++ code (written by somebody else) and found this section: double someValue = ... if (someValue < std::numeric_limits::epsilon() && someValue > -std::numeric_limits::epsilon()) { someValue =…
Sebastian Krysmanski
  • 8,114
  • 10
  • 49
  • 91
208
votes
15 answers

C# Double - ToString() formatting with two decimal places but no rounding

How do I format a Double to a String in C# so as to have only two decimal places? If I use String.Format("{0:0.00}%", myDoubleValue) the number is then rounded and I want a simple truncate without any rounding. I also want the conversion to String…
kjv
  • 11,047
  • 34
  • 101
  • 140
205
votes
9 answers

How to round a Double to the nearest Int in swift?

I'm trying to make a calculator of growth rate (Double) that will round the result to the nearest Integer and recalculate from there, as such: let firstUsers = 10.0 let growth = 0.1 var users = firstUsers var week = 0 while users < 14 { …
duarte harris
  • 2,203
  • 2
  • 11
  • 9
202
votes
18 answers

How do I convert a double into a string in C++?

I need to store a double as a string. I know I can use printf if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
194
votes
3 answers

Why is a round-trip conversion via a string not safe for a double?

Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent: double d1 = 0.84551240822557006; string s = d1.ToString("R"); double d2 = double.Parse(s); bool s1 = d1 == d2; // -> s1 is False But…
Philip Ding
  • 1,571
  • 2
  • 11
  • 11
184
votes
24 answers

Retain precision with double in Java

public class doublePrecision { public static void main(String[] args) { double total = 0; total += 5.6; total += 5.8; System.out.println(total); } } The above code prints: 11.399999999999 How would I get…
Deinumite
  • 3,979
  • 3
  • 25
  • 22
178
votes
11 answers

How to implement infinity in Java?

Does Java have anything to represent infinity for every numerical data type? How is it implemented such that I can do mathematical operations with it? E.g. int myInf = infinity; //However it is done myInf + 5; //returns infinity myInf*(-1);…
user1753100
  • 1,909
  • 2
  • 14
  • 12
170
votes
2 answers

Android - Round to 2 decimal places

Possible Duplicate: Round a double to 2 significant figures after decimal point I know that there are plenty of examples on how to round this kind numbers. But could someone show me how to round double, to get value that I can display as a…
goodm
  • 7,275
  • 6
  • 31
  • 55
154
votes
3 answers

Why does adding 0.1 multiple times remain lossless?

I know the 0.1 decimal number cannot be represented exactly with a finite binary number (explanation), so double n = 0.1 will lose some precision and will not be exactly 0.1. On the other hand 0.5 can be represented exactly because it is 0.5 = 1/2 =…
icza
  • 389,944
  • 63
  • 907
  • 827
149
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
  • 11,770
  • 11
  • 62
  • 80