Questions tagged [double]

Double is a primitive data type used to store fractional numbers that holds a double-precision floating-point (often 64 bits).

double
Filter by
Sorted by
Tagged with
2447 votes
18 answers
1.2m views

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?
user avatar
1015 votes
7 answers
591k views

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

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 ...
Soni Ali's user avatar
  • 18.7k
806 votes
13 answers
77k views

How to convert Decimal to Double in C#?

I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal trans = trackBar1.Value / 5000; this.Opacity = trans; When I build the app it gives ...
616 votes
29 answers
843k views

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 ...
Pyrolistical's user avatar
  • 27.8k
601 votes
13 answers
1.7m views

Round a double to 2 decimal places [duplicate]

If the value is 200.3456, it should be formatted to 200.34. If it is 200, then it should be 200.00.
Rajesh's user avatar
  • 8,557
598 votes
5 answers
1.5m views

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 <stdio.h> int main() { double d = 1.4; printf(&...
Leopard's user avatar
  • 5,991
592 votes
5 answers
79k views

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); ...
Peter Lawrey's user avatar
552 votes
36 answers
599k views

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) ...
Leighton's user avatar
  • 6,659
422 votes
21 answers
468k views

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 <math.h>, which doesn't exist in <cmath>, which I was #includeing at ...
hasen's user avatar
  • 164k
421 votes
8 answers
469k views

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 ...
Truong Ha's user avatar
  • 10.7k
374 votes
9 answers
345k views

How can I divide two integers to get a double?

How do I divide two integers to get a double?
leora's user avatar
  • 192k
352 votes
8 answers
595k views

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 ...
sanjeev40084's user avatar
  • 9,397
329 votes
14 answers
1.4m views

Convert String to double in Java

How can I convert a String such as "12.34" to a double in Java?
TinyBelly's user avatar
  • 3,357
320 votes
7 answers
233k views

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's user avatar
  • 58.5k
313 votes
17 answers
473k views

How do I print a double value without scientific notation using Java?

I want to print a double value in Java without exponential form. double dexp = 12345678; System.out.println("dexp: "+dexp); It shows this E notation: 1.2345678E7. I want it to print it like this: ...
Despicable's user avatar
  • 3,887
306 votes
11 answers
299k views

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; ...
Franck Freiburger's user avatar
301 votes
12 answers
67k views

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 ...
Jamie Ide's user avatar
  • 48.8k
285 votes
19 answers
444k views

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. ...
user avatar
264 votes
3 answers
618k views

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's user avatar
  • 156k
255 votes
9 answers
386k views

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 ...
Leo's user avatar
  • 5,127
243 votes
5 answers
276k views

Can't use modulus on doubles?

I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error: error: invalid operands of types 'double' and '...
Bhaxy's user avatar
  • 5,424
226 votes
12 answers
85k views

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<double>::epsilon() && ...
Sebastian Krysmanski's user avatar
215 votes
15 answers
543k views

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 ...
kjv's user avatar
  • 11.2k
207 votes
9 answers
216k views

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 = ...
duarte harris's user avatar
206 votes
19 answers
497k views

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 ...
Bill the Lizard's user avatar
196 votes
3 answers
11k views

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(...
Philip Ding's user avatar
  • 1,591
185 votes
24 answers
264k views

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 ...
Deinumite's user avatar
  • 3,999
182 votes
11 answers
459k views

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 ...
user1753100's user avatar
  • 1,959
172 votes
2 answers
256k views

Android - Round to 2 decimal places [duplicate]

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 ...
goodm's user avatar
  • 7,285
154 votes
3 answers
18k views

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 ...
icza's user avatar
  • 403k
151 votes
5 answers
270k views

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 ...
Wouter Dorgelo's user avatar
146 votes
20 answers
168k views

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 ...
林鼎棋's user avatar
  • 2,045
144 votes
15 answers
175k views

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's user avatar
  • 4,218
143 votes
16 answers
207k views

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 ...
tim_yng's user avatar
  • 2,621
143 votes
3 answers
510k views

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 ...
vdMandele's user avatar
  • 1,629
140 votes
3 answers
10k views

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 ...
gvlasov's user avatar
  • 19.3k
139 votes
13 answers
596k views

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's user avatar
  • 1,597
139 votes
5 answers
291k views

Dealing with float precision in Javascript [duplicate]

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 ...
Jeroen Ooms's user avatar
  • 32.5k
138 votes
13 answers
196k views

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's user avatar
  • 2,947
136 votes
8 answers
321k views

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's user avatar
  • 14.7k
136 votes
10 answers
212k views

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 ...
Jahoe's user avatar
  • 1,676
132 votes
12 answers
11k views

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 { ...
Thirler's user avatar
  • 20.5k
132 votes
10 answers
73k views

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: ...
rxmnnxfpvg's user avatar
  • 30.6k
127 votes
7 answers
117k views

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 ...
dtech's user avatar
  • 13.9k
126 votes
11 answers
305k views

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(...
Steve Armstrong's user avatar
124 votes
18 answers
394k views

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's user avatar
  • 1,417
119 votes
5 answers
213k views

How to print a double with two decimals in Android? [duplicate]

Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem: I have a var: ...
ArcDare's user avatar
  • 3,106
119 votes
3 answers
59k views

C# 6 how to format double using interpolated string?

I have used interpolated strings for messages containing string variables like $"{EmployeeName}, {Department}". Now I want to use an interpolated string for showing a formatted double. ...
MagB's user avatar
  • 2,191
112 votes
7 answers
56k views

In java, is it more efficient to use byte or short instead of int and float instead of double?

I've noticed I've always used int and doubles no matter how small or big the number needs to be. So in java, is it more efficient to use byte or short instead of int and float instead of double? So ...
DisibioAaron's user avatar
  • 1,362
110 votes
1 answer
109k views

Set precision of std::to_string when converting floating point values [duplicate]

In C++11, std::to_string defaults to 6 decimal places when given an input value of type float or double. What is the recommended, or most elegant, method for changing this precision?
learnvst's user avatar
  • 15.9k

1
2 3 4 5
167