double

Java Double to String conversion without formatting

百般思念 提交于 2019-12-17 16:32:12
问题 I have the number 654987. Its an ID in a database. I want to convert it to a string. The regular Double.ToString(value) makes it into scientific form, 6.54987E5. Something I dont want. Other formatting functions Ive found checks the current locale and adds appropriate thousand separators and such. Since its an ID, I cant accept any formatting at all. How to do it? [Edit] To clarify: Im working on a special database that treats all numeric columns as doubles. Double is the only (numeric) type

Check if string is a valid double value in Swift

随声附和 提交于 2019-12-17 16:28:15
问题 In Swift, how can one check if a string is a valid double value? I have been using the following extension from this question (but as a float) but if the value cannot be converted, it simply returns "0": extension String { var doubleValue:Double? { return (self as NSString).doubleValue } } Ideally, I would like it to return nil so it can be caught in an if-let , like so: if let i = str.doubleValue { object.price = i } else { // Tell user the value is invalid } 回答1: It is indeed more efficient

Double subtraction precision issue

旧城冷巷雨未停 提交于 2019-12-17 15:46:31
问题 My coworker did this experiment: public class DoubleDemo { public static void main(String[] args) { double a = 1.435; double b = 1.43; double c = a - b; System.out.println(c); } } For this first-grade operation I expected this output: 0.005 But unexpectedly the output was: 0.0050000000000001155 Why does double fails in such a simple operation? And if double is not the datatype for this work, what should I use? 回答1: double is internally stored as a fraction in binary -- like 1/4 + 1/8 + 1/16 +

Storing statistical data, do I need DECIMAL, FLOAT or DOUBLE?

无人久伴 提交于 2019-12-17 15:35:21
问题 I am creating for fun, but I still want to approach it seriously, a site which hosts various tests. With these tests I hope to collect statistical data. Some of the data will include the percentage of the completeness of the tests as they are timed. I can easily compute the percentage of the tests but I would like true data to be returned as I store the various different values concerning the tests on completion. Most of the values are, in PHP floats, so my question is, if I want true

What's wrong with this division? [closed]

拥有回忆 提交于 2019-12-17 14:55:49
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I think there's a lot for me to learn about data types. Why this happens double result = ((3/8)*100).ToString(); it gives zero .. should be 37,5 ... :(

Formatting a string into columns using String Interpolation

牧云@^-^@ 提交于 2019-12-17 14:54:57
问题 I need to print doubles so that definite number of symbols (like 8) is allocated for string representation of value. Next words should start at same index from beginning of string in each string. Now I have: value: 0 test value: 0.3333333333333 test value: 0.5 test I need: value: 0 test value: 0.33333333 test value: 0.5 test Test code: double[] ar = new double[] { 0, (double)1 / 3, (double)1 / 2 }; string s = "test"; foreach (var d in ar) { Console.WriteLine($"value: {d} {s}"); } What should

Double vs Decimal Rounding in C#

柔情痞子 提交于 2019-12-17 14:47:10
问题 Why does: double dividend = 1.0; double divisor = 3.0; Console.WriteLine(dividend / divisor * divisor); output 1.0, but: decimal dividend = 1; decimal divisor = 3; Console.WriteLine(dividend / divisor * divisor); outputs 0.9999999999999999999999999999 ? I understand that 1/3 can't be computed exactly, so there must be some rounding. But why does Double round the answer to 1.0, but Decimal does not? Also, why does double compute 1.0/3.0 to be 0.33333333333333331? If rounding is used, then

关于JVM中long和double的读取原子性

为君一笑 提交于 2019-12-17 14:21:47
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 今天看《Java并发编程实战》的书中,关于long和double的原子性有这么一段话,意思就是在JVM中,对于32位(或者以下)的数值变量都是原子性读写,但是对于long和double这种64位的操作是非原子性。分成两次32位的操作。 在以下这种操作中就会出现读取的数值错误。 线程A先写高位32位操作, 线程B读高位32位, 线程B读地位32位, 线程A写地位32位。 那么解决办法就是对变量加上volatile关键字,volatile关键字有3个主要功能,第一个就是可见性,每次直接从内存读写。第二个就是禁止指令重排序,在JVM中会对指令进行优化,优化的时候可能会对指令进行排序。第三个就是对volatile修饰的变量读取都是原子性的。 但是书上没有说操作系统的位数和JVM的位数。 所以我猜想,只有64位JVM+64位操作系统+64位硬件才能实现对64位long和double的原子性读取。 来源: oschina 链接: https://my.oschina.net/u/2250599/blog/533108

C# Converting 20 digit precision double to string and back again

只愿长相守 提交于 2019-12-17 10:58:15
问题 In C#. I have a double (which I've extracted from a database) that has 20 digit precision. In Visual Studio (using QuickWatch) I can see the value of the double to be = 0.00034101243963859839. I want to display this value in a textbox and then have it be the same value when I take it out and convert it back into a double. But I always lose the last two digits I've tried the following: double d = 0.00034101243963859839; string s = d.ToString(); string s2 = d.ToString("F20"); string s3 = d

Converting a double to an int in C#

老子叫甜甜 提交于 2019-12-17 10:33:26
问题 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 . 回答1: Because Convert.ToInt32 rounds: Return Value: rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6. ...while the cast truncates: When you