double

String to Double conversion C++ [duplicate]

感情迁移 提交于 2020-01-14 13:05:49
问题 This question already has answers here : How do I print a double value with full precision using cout? (10 answers) Closed 5 years ago . I'm trying to convert a string into a double but my double gets cut off at the 3rd decimal point. My string looks like this: "-122.39381636393" After it gets converted it looks like this: -122.394 void setLongitude(string longitude){ this->longitude = (double)atof(longitude.c_str()); cout << "got longitude: " << longitude << endl; cout << "setting longitude:

Is there a better way to convert a Double to it's exact decimal String?

£可爱£侵袭症+ 提交于 2020-01-13 19:17:07
问题 I tried many ways to convert a Double to it's exact decimal String with String.format() and DecimalFormater and I found this so far : String s = "-.0000000000000017763568394002505"; Double d = Double.parseDouble(s) * 100; System.out.println((new java.text.DecimalFormat("###########################.################################################################################")).format(d)); (https://ideone.com/WG6zeC) I'm not really a fan of my solution because it's ugly to me to make a huge

How to check if number is divisible in c#?

浪尽此生 提交于 2020-01-13 18:23:30
问题 I need to know how to do this procedure. calculation1: 1/4 = 0,25 calculation2: 1/8 = 0,125 calculation3: 47/183 = 0,25683060109289617486338797814207...... calculation4: 58/889 = 0,06524184476940382452193475815523...... calculation5: 1/5 = 0,2 The results of calculations 1, 2 and 5 will give a short result, no periods or and endless string of digits. The results of calculations 3 and 4 are very long and complicated. How can I check which calculation is an "easy one" and gives a "short" result

How to check if number is divisible in c#?

不打扰是莪最后的温柔 提交于 2020-01-13 18:23:03
问题 I need to know how to do this procedure. calculation1: 1/4 = 0,25 calculation2: 1/8 = 0,125 calculation3: 47/183 = 0,25683060109289617486338797814207...... calculation4: 58/889 = 0,06524184476940382452193475815523...... calculation5: 1/5 = 0,2 The results of calculations 1, 2 and 5 will give a short result, no periods or and endless string of digits. The results of calculations 3 and 4 are very long and complicated. How can I check which calculation is an "easy one" and gives a "short" result

JSpinner and double with dot or comma as separator

你说的曾经没有我的故事 提交于 2020-01-13 10:50:08
问题 I would like to allow both "comma" and "dot" as separator in double. I could use replace method in string to get only one separator, but problem is that double value is value of JSpinner and I was not able to find any method to allow both separators. If I set locale for example to French only one separator is allowed. 回答1: Just use a custom formatter for the JFormattedTextField of the DefaultEditor of the JSpinner , like the code below: import java.text.ParseException; import javax.swing

Java double to string with specific precision

六眼飞鱼酱① 提交于 2020-01-13 07:54:07
问题 I would like to convert double into String . I want it to have as few digits as possible and maximally 6. So I found String.format("%.6f", d) which converts my 100.0 into 100.000000 . Max precision works correctly, but I would like it to be converted to 100 (minimum precision). Have you got any idea what method is working like that? 回答1: Use DecimalFormat : new DecimalFormat("#.0#####").format(d) . This will produce numbers with 1 to 6 decimal digits. Since DecimalFormat will use the symbols

How should I compare these doubles to get the desired result?

浪子不回头ぞ 提交于 2020-01-11 12:43:05
问题 I have a simple example application here where I am multiplying and adding double variables and then comparing them against an expected result. In both cases the result is equal to the expected result yet when I do the comparison it fails. static void Main(string[] args) { double a = 98.1; double b = 107.7; double c = 92.5; double d = 96.5; double expectedResult = 88.5; double result1 = (1*2*a) + (-1*1*b); double result2 = (1*2*c) + (-1*1*d); Console.WriteLine(String.Format("2x{0} - {1} = {2}

In C#, why am i getting “can't cast double to float error”?

谁都会走 提交于 2020-01-11 10:57:48
问题 I have the following line of code: float top = shape.Y + (shape.Height / 2.0) - 4.5; whic is failing with the error' Can't cast double to float. Shape.Y and shape.height are both type float. What is causing this error and what is the best was to make sure top is a float (as i need to pass it into another function tha expects a float. 回答1: Try to wrap your numbers with f , as normally 2.0 would represent a double number. You can read more about float here. float top = shape.Y + (shape.Height /

Linq: List of double values - differences between successor values

久未见 提交于 2020-01-11 09:26:11
问题 i have a list of double values... 1.23, 1.24, 1.78, 1,74... so i want to calculate the differences between the successor -> only adding (negative values should be firstly positive)... above 4 values would be 0,01 +0,53 (-)-0,04 (-) -> to make it positive... with an for-loop, it is easy... any idea how to solve it with linq? 回答1: I'm not sure what you mean about the negative bits, but this might do what you want. It's horrible because it uses side effects, but... double prev = 0d; var

Floating Point Arithmetic error

。_饼干妹妹 提交于 2020-01-11 08:46:30
问题 I'm using the following function to approximate the derivative of a function at a point: def prime_x(f, x, h): if not f(x+h) == f(x) and not h == 0.0: return (f(x+h) - f(x)) / h else: raise PrecisionError As a test I'm passing f as fx and x as 3.0. Where fx is: def fx(x): import math return math.exp(x)*math.sin(x) Which has exp(x)*(sin(x)+cos(x)) as derivative. Now, according to Google and to my calculator exp(3)*(sin(3)+cos(3)) = -17.050059 . So far so good. But when I decided to test the