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
71
votes
21 answers

How do I round a double to two decimal places in Java?

This is what I did to round a double to 2 decimal places: amount = roundTwoDecimals(amount); public double roundTwoDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.##"); return Double.valueOf(twoDForm.format(d)); } This…
sherry
  • 1,829
  • 7
  • 21
  • 23
70
votes
5 answers

Are all integer values perfectly represented as doubles?

My question is whether all integer values are guaranteed to have a perfect double representation. Consider the following code sample that prints "Same": // Example program #include #include int main() { int a = 3; int b =…
Thomas
  • 4,696
  • 5
  • 36
  • 71
69
votes
5 answers

Why doesn't Java throw an Exception when dividing by 0.0?

I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are doubles. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when…
froadie
  • 79,995
  • 75
  • 166
  • 235
69
votes
5 answers

How to get absolute value from double - c-language

I want the absolute-value from a negative double - and I thought the abs-function was as easy to use as in java - but NOT! It seems that the abs-function returns an int because I have the value 3.8951 and the output is 3.000000 double d1 =…
user3155478
  • 975
  • 1
  • 10
  • 16
68
votes
3 answers

Converting Decimal to Double in C#?

I have a variable which is storing as decimal: decimal firststYrComp = Int16.Parse(tb1stYr.Text.ToString()); Now I have this to get typecasted into Double? How do I do that? Thanks!
RG-3
  • 6,088
  • 19
  • 69
  • 125
65
votes
8 answers

double or float, which is faster?

I am reading "accelerated C++". I found one sentence which states "sometimes double is faster in execution than float in C++". After reading sentence I got confused about float and double working. Please explain this point to me.
coming out of void
  • 1,454
  • 2
  • 12
  • 12
64
votes
10 answers

Comparator with double type

I have written the following code: public class NewClass2 implements Comparator { public int compare(Point p1, Point p2) { return (int)(p1.getY() - p2.getY()); } } If I let's say have two double numbers, 3.2 - 3.1, the…
user472221
  • 3,014
  • 11
  • 35
  • 42
64
votes
12 answers

Checking if a double value is an integer - Swift

I need to check if a double-defined variable is convertible to Int without losing its value. This doesn't work because they are of different types: if self.value == Int(self.value) where self.value is a double.
Youssef Moawad
  • 2,846
  • 5
  • 28
  • 50
63
votes
6 answers

Java:Why should we use BigDecimal instead of Double in the real world?

When dealing with real world monetary values, I am advised to use BigDecimal instead of Double.But I have not got a convincing explanation except, "It is normally done that way". Can you please throw light on this question?
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
62
votes
5 answers

Which sql server data type best represents a double in C#?

Is it money, float, real, decimal, _________ ?
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
61
votes
8 answers

Limiting double to 3 decimal places

This i what I am trying to achieve: If a double has more than 3 decimal places, I want to truncate any decimal places beyond the third. (do not round.) Eg.: 12.878999 -> 12.878 If a double has less than 3 decimals, leave unchanged Eg.: 125 ->…
Ayush
  • 41,754
  • 51
  • 164
  • 239
60
votes
3 answers

How to format Double with dot?

How do I format a Double with String.format to String with a dot between the integer and decimal part? String s = String.format("%.2f", price); The above formats only with a comma: ",".
Shikarn-O
  • 3,337
  • 7
  • 26
  • 27
59
votes
7 answers

C++ string to double conversion

Usually when I write anything in C++ and I need to convert a char into an int I simply make a new int equal to the char. I used the code(snippet) string word; openfile >> word; double lol=word; I receive the error that Code1.cpp cannot…
TimeCoder
  • 1,379
  • 3
  • 12
  • 13
59
votes
6 answers

What do F and D mean at the end of numeric literals?

I've seen some of this symbols, but I cannot find anything strange with it, double d = 5D; float f = 3.0F; What does the D and F behind 5 exactly means?
Ben C.
  • 1,148
  • 3
  • 13
  • 17
57
votes
7 answers

Decimal vs Double Speed

I write financial applications where I constantly battle the decision to use a double vs using a decimal. All of my math works on numbers with no more than 5 decimal places and are not larger than ~100,000. I have a feeling that all of these can be…
Superman
  • 3,686
  • 6
  • 34
  • 46