double

How to read double in java from a file

旧时模样 提交于 2019-12-12 20:37:16
问题 I'm trying to read a double from a file but I have this exception: java.util.InputMismatchException. I've tried to do the useLocale(Locale.US) but it doesn't work. This is my code public static void main(String[] args){ System.out.println("Introduce the name of the file"); Scanner teclat = new Scanner(System.in); teclat.useLocale(Locale.US); Scanner fitxer = new Scanner(new File(teclat.nextLine())); while(fitxer.hasNext()){ String origen=fitxer.next(); String desti=fitxer.next(); double

Lowest Common Multiple with doubles in C

狂风中的少年 提交于 2019-12-12 18:14:12
问题 I am doing an assignment for a Coursera class that asks me to calculate the Lowest Common Multiple of two numbers, either of which are no larger than 2 * 10 ^ 9. I'm writing this in C and I'm running my code on a test case with the numbers 226553150 and 1023473145. The answer is 46374212988031350, but I'm getting 46374212988031344, which is off by 6! I've written a correct solution in Python that uses essentially the same approach as the one I've posted below, but the details of numeric

Casting float to string without scientific notation

99封情书 提交于 2019-12-12 16:23:54
问题 The float: fl = 0.000005 casts to String as str(fl)=='5e-06' . however, I want it to cast as str(fl)='0.000005' for exporting to CSV purposes. How do I achieve this? 回答1: Use fl = 0.00005 s = str('%8.5f' % fl) print s, type(s) Gives 0.00005 <type 'str'> In case you want no extra digits, use %g fl = 0.0005 s = str('%g' % fl) print s, type(s) fl = 0.005 s = str('%g' % fl) print s, type(s) Gives 0.0005 <type 'str'> 0.005 <type 'str'> 回答2: You can just use the standard string formatting option

Android - avoid exponential in string to double conversion

狂风中的少年 提交于 2019-12-12 15:53:17
问题 I trying to get value from edittext and stored as string. Then it converts to double. While converting up to 7 characters functioning normally but if i try to add more than 7 result is 1.23456789E8. Here is my code String value = txtData.txtSearch.getText().toString(); // value = "987654321.45" double amount1 = Double.parseDouble(value); // amount1 = 9.8765432145E8 double amount2 = 0.05; double result = (amount1 * amount2) / 100; // result = 4.3827160725E14 after calculation i got 4

Strange if-statement behavior with zero value double

时间秒杀一切 提交于 2019-12-12 12:32:47
问题 Would anyone care to explain to me how the value of this.oBalance.QouteBalance is evaluated to be true for being less than zero when it clearly isn't? Please see image below. Am I missing something fundamental when it comes to comparing doubles in C#?? public double QouteBalance { get; set; } UpdateBalance_PositionOpenned() is not being called in a loop, but is being called as part of a more complex event driven procedure that runs on the ticks of a timer (order of milliseconds) EDIT: Pardon

Return C++ double to Python?

霸气de小男生 提交于 2019-12-12 11:11:17
问题 So I am using python to call methods in a shared C++ library. I am having an issue returning a double from the C++ to the python. I have a created a toy example that exhibits the problem. Feel free to compile and try it out. Here is the python code (soexample.py): # Python imports from ctypes import CDLL import numpy as np # Open shared CPP library: cpplib=CDLL('./libsoexample.so') cppobj = cpplib.CPPClass_py() # Stuck on converting to short**? x = cpplib.func_py(cppobj) print 'x =', x Here

How to display multiple leading zeros for floating point values in C++? [duplicate]

99封情书 提交于 2019-12-12 10:47:29
问题 This question already has answers here : How can I pad an int with leading zeros when using cout << operator? (6 answers) Closed 4 years ago . In a C++ program, I want to display a column of floating point values so that the sign, digits, and decimal point all line up. Multiple leading zeros should pad the whole number part of each value, when necessary. For example: A column of floating point values: +000.0012 -000.0123 +000.1235 -001.2346 +012.3457 -123.4568 I had an elaborately commented

Set the number of digits after the point in double

孤街浪徒 提交于 2019-12-12 10:46:19
问题 In c# double type how can i set the number of digits after the point, i need only 4. thank you. 回答1: You can't. Binary floating point doesn't work like that. You can format a double that way (e.g. using "f4" as the format string), but if you're dealing with values which have a natural number of decimal places, then you should probably be using decimal instead. Could you give us more information about what your values represent? 回答2: You can't set the number of digits after the point on the

Java: Math.random() Max Value (double just less than 1)

点点圈 提交于 2019-12-12 10:44:49
问题 I've been a little curious about this. Math.random() gives a value in the range [0.0,1.0). So what might the largest value it can give be? In other words, what is the closest double value to 1.0 that is less than 1.0? 回答1: Java uses 64-bit IEEE-754 representation, so the closest number smaller than one is theoretically 3FEFFFFFFFFFFFFF in hexadecimal representation, which is 0 for sign, -1 for the exponent, and 1.9999999999999997 for the 52-bit significand. This equals to roughly 0

Convert double to int in C++ without round down errors

回眸只為那壹抹淺笑 提交于 2019-12-12 10:39:01
问题 I have the following codes to cast a double into an int : double dblValue = 7.30; int intValue = (int)(dblValue*100); //I want intValue to store extactly 730; cout << intValue; Output: 729 I know that the compiler is reading dblValue as 7.2999999 before casting it to int. My question is : Is it possible to cast it as 730 by preventing the round down errors? It would be best if your solution avoid using C++11 or other predefined functions. The only pre-processor directive I am using here is