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
2192
votes
18 answers

Difference between decimal, float and double in .NET?

What is the difference between decimal, float and double in .NET? When would someone use one of these?
Tom
930
votes
7 answers

decimal vs double! - Which one should I use and when?

I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision. My question is when should a use a double and when should I use a decimal type? Which type is suitable for money computations? (ie. greater…
Soni Ali
  • 17,252
  • 15
  • 42
  • 49
718
votes
14 answers

How to convert a Decimal to a Double in C#?

I want to use a Track-Bar to change a Form's opacity. This is my code: decimal trans = trackBar1.Value / 5000; this.Opacity = trans; When I build the application, it gives the following error: Cannot implicitly convert type decimal to double I…
Eggs McLaren
  • 1,787
  • 3
  • 12
  • 13
579
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
  • 498,481
  • 72
  • 700
  • 1,075
542
votes
27 answers

How to nicely format floating numbers to string without unnecessary decimal 0's

A 64-bit double can represent integer +/- 253 exactly. Given this fact, I choose to use a double type as a single type for all my types, since my largest integer is an unsigned 32-bit number. But now I have to print these pseudo integers, but the…
Pyrolistical
  • 26,088
  • 21
  • 78
  • 104
527
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
  • 7,657
  • 5
  • 18
  • 7
523
votes
5 answers

Correct format specifier for double in printf

What is the correct format specifier for double in printf? Is it %f or is it %lf? I believe it's %f, but I am not sure. Code sample #include int main() { double d = 1.4; printf("%lf", d); // Is this wrong? }
Leopard
  • 5,241
  • 3
  • 13
  • 4
466
votes
29 answers

Rounding a double value to x number of decimal places in swift

Can anyone tell me how to round a double value to x number of decimal places in Swift? I have: var totalWorkTimeInHours = (totalWorkTime/60/60) With totalWorkTime being an NSTimeInterval (double) in second. totalWorkTimeInHours will give me the…
Leighton
  • 5,669
  • 6
  • 19
  • 27
386
votes
20 answers

Checking if a double (or float) is NaN in C++

Is there an isnan() function? PS.: I'm in MinGW (if that makes a difference). I had this solved by using isnan() from , which doesn't exist in , which I was #includeing at first.
hasen
  • 148,751
  • 62
  • 182
  • 223
343
votes
6 answers

Double vs. BigDecimal?

I have to calculate some floating point variables and my colleague suggest me to use BigDecimal instead of double since it will be more precise. But I want to know what it is and how to make most out of BigDecimal?
Truong Ha
  • 9,070
  • 10
  • 35
  • 45
313
votes
7 answers

How can I divide two integers to get a double?

How do I divide two integers to get a double?
leora
  • 163,579
  • 332
  • 834
  • 1,328
306
votes
14 answers

Convert String to double in Java

How can I convert a String such as "12.34" to a double in Java?
TinyBelly
  • 3,127
  • 3
  • 14
  • 7
297
votes
7 answers

How do you test to see if a double is equal to NaN?

I have a double in Java and I want to check if it is NaN. What is the best way to do this?
Eric Wilson
  • 51,818
  • 71
  • 192
  • 262
289
votes
8 answers

Round double in two decimal places in C#?

I want to round up double value in two decimal places in c# how can i do that? double inputValue = 48.485; after round up inputValue = 48.49; Related: c# - How do I round a decimal value to 2 decimal places (for output on a page)
sanjeev40084
  • 7,999
  • 17
  • 62
  • 93
276
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
  • 45,803
  • 16
  • 74
  • 115
1
2 3
99 100