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
77
votes
6 answers

Converting string to double in C#

I have a long string with double-type values separated by # -value1#value2#value3# etc I splitted it to string table. Then, I want to convert every single element from this table to double type and I get an error. What is wrong with type-conversion…
whoah
  • 4,013
  • 10
  • 47
  • 81
76
votes
2 answers

Why does Math.ceil return a double?

When I call Math.ceil(5.2) the return is the double 6.0. My natural inclination was to think that Math.ceil(double a) would return a long. From the documentation: ceil(double a) Returns the smallest (closest to negative infinity) double value …
PengOne
  • 47,332
  • 17
  • 126
  • 147
74
votes
5 answers

DOUBLE vs DECIMAL in MySQL

OK, so I know there are tons of articles stating I shouldn't use DOUBLE to store money on a MySQL database, or I'll end up with tricky precision bugs. The point is I am not designing a new database, I am ask to find way to optimise an existing…
user327961
  • 2,188
  • 3
  • 18
  • 19
74
votes
6 answers

How do I get DOUBLE_MAX?

AFAIK, C supports just a few data types: int, float, double, char, void enum. I need to store a number that could reach into the high 10 digits. Since I'm getting a low 10 digit # from INT_MAX , I suppose I need a double. doesn't have…
P.Brian.Mackey
  • 39,360
  • 59
  • 210
  • 327
73
votes
6 answers

Could not cast value of type 'NSTaggedPointerString' to 'NSNumber'

I have a Swift struct like this. struct Usage { var totalData: Double var remainingTotalData: Double init(jsonData: NSData) { var jsonDict = [String: AnyObject]() do { jsonDict = try…
Isuru
  • 27,485
  • 56
  • 174
  • 278
73
votes
6 answers

How to change symbol for decimal point in double.ToString()?

I would like to change decimal point to another character in C#. I have a double variable value double value; and when I use the command: Console.WriteLine(value.ToString()); // output is 1,25 I know I can do…
Martin Vseticka
  • 25,160
  • 25
  • 118
  • 187
73
votes
8 answers

Convert double to BigDecimal and set BigDecimal Precision

In Java, I want to take a double value and convert it to a BigDecimal and print out its String value to a certain precision. import java.math.BigDecimal; public class Main { public static void main(String[] args) { double d=-.00012; …
c12
  • 8,765
  • 43
  • 140
  • 243
71
votes
5 answers

Round to nearest five

I need to round a double to nearest five. I can't find a way to do it with the Math.Round function. How can I do this? What I want: 70 = 70 73.5 = 75 72 = 70 75.9 = 75 69 = 70 and so on.. Is there an easy way to do this?
Martin
  • 2,039
  • 2
  • 18
  • 10
70
votes
6 answers

Java double vs BigDecimal for latitude/longitude

When storing latitudes/longitudes which are typically of the format: 44.087585 (i.e. max 2 numbers before the dot and 6dp) do I need to bother with bigdecimals?
DD.
  • 19,793
  • 49
  • 140
  • 237
68
votes
3 answers

Unsigned double in C++?

Why doesn't C++ support unsigned double syntax?
lost3den
  • 745
  • 1
  • 5
  • 6
68
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
  • 71,770
  • 69
  • 154
  • 228
67
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,809
  • 7
  • 21
  • 23
67
votes
17 answers

Comparing double values in C#

I've a double variable called x. In the code, x gets assigned a value of 0.1 and I check it in an 'if' statement comparing x and 0.1 if (x==0.1) { ---- } Unfortunately it does not enter the if statement Should I use Double or double? What's the…
stack_pointer is EXTINCT
  • 2,003
  • 9
  • 45
  • 56
65
votes
7 answers

Random weighted selection in Java

I want to choose a random item from a set, but the chance of choosing any item should be proportional to the associated weight Example inputs: item weight ---- ------ sword of misery 10 shield of happy …
yosi
  • 685
  • 1
  • 7
  • 5
65
votes
7 answers

Converting double to string with N decimals, dot as decimal separator, and no thousand separator

I need to convert a decimal to a string with N decimals (two or four) and NO thousand separator: 'XXXXXXX (dot) DDDDD' The problem with CultureInfo.InvariantCulture is that is places ',' to separate thousands. UPDATE This should work for decimal and…
Captain Comic
  • 14,130
  • 42
  • 99
  • 140