double

Only show decimal portion of Double with the decimal point

岁酱吖の 提交于 2019-12-08 09:01:27
I'm trying to only show the decimal portion of a number but it keeps printing the number with the decimal point: for number 223.50: changeAmountLabel.text = "\(balance.truncatingRemainder(dividingBy: 1))" this will print .5, but what needs to be printed is 50, is there a way to do this? You can do this by using a NumberFormatter . Try this: let formatter = NumberFormatter() formatter.locale = Locale(identifier: "en_US_POSIX") formatter.minimumFractionDigits = 2 formatter.maximumFractionDigits = 3 let number = 223.50 if let str = formatter.string(from: NSNumber(value: number)) as? String { if

How to properly iterate over a double

点点圈 提交于 2019-12-08 09:00:16
问题 I'm iterating over t like this double dt = 0.1, t; double tmax = 100; for (t = 0; t <= tmax; t += dt) { /*do something*/ } If dt is 0.1, as here, everything works as it should and the step is executed for t = 100 . But if I decrease the step, for example dt = 0.001 , the last step is not executed. How should I properly iterate over doubles? 回答1: Iterate over an integer and use a start-step formula to get each double value. double dt = 0.1, t; double tmax = 100; int i, n = (int)(tmax / dt); //

Java : How to count the number of decimals contained in a double value that represent money?

扶醉桌前 提交于 2019-12-08 08:45:10
问题 For a change-making application, made with Java, I am facing a problem while counting the number of decimals in a double value. The double value represent money, so all my operations must be made in consequences that a double value like 12.58 can be stored as 12.5797886 after an operation is made on the original value. To counter this effect, I chose to use String, just like this : String amountString = String.valueOf(amount); String decimals = amountString.substring(amountString.indexOf('.')

Java 6 - Creating and detecting the first Double value above Float.MAX_VALUE

非 Y 不嫁゛ 提交于 2019-12-08 06:57:26
I would like to create the Double whose value is closest to, but greater than, Float.MAX_VALUE . I've just written a question similar to this but for Double and Long.MAX_VALUE , see here . How can I repeat the conversion for Double and Float.MAX_VALUE using the standard Java 6 API? My attempt is below, but is incorrect it seems: Long longValue = Long.valueOf(Float.floatToIntBits(Float.MAX_VALUE)); Double value = Double.longBitsToDouble(Double.doubleToLongBits(longValue)+1); if (value < -Float.MAX_VALUE || value > Float.MAX_VALUE) { // Code here should execute but does not. } Sincere thanks.

Swift 3.0 convert Double() to NSMutableAttributedString

余生颓废 提交于 2019-12-08 06:28:27
问题 in advance thanks for help. I am trying to make calculator application (for specific purposes) and I would like to know, if there exist a way how to convert Double() to NSMutableAttributedString. I need this for label output answer. Reason of using NSMutableAttributedString is because I would like to have answer with subscripts and upper-scripts. //example of my code var a = Double(), b = Double(), c = Double() a = Double(textField1.text!) b = Double(textField2.text!) c = a + b let font

binary representation of a float

女生的网名这么多〃 提交于 2019-12-08 04:22:31
why this two call to toBinary function compute the same output (at least under VS2010) ? #include <iostream> #include <bitset> #include <limits> using namespace std; template<class T> bitset<sizeof(T)*CHAR_BIT> toBinary(const T num) { bitset<sizeof(T)*CHAR_BIT> mybits; const char * const p = reinterpret_cast<const char*>(&num); for (int i = sizeof(T)*CHAR_BIT-1 ; i >= 0 ; --i) mybits.set(i, (*(p)&(1<<i) )); return mybits; } int main() { cout << toBinary(8.9).to_string() << "\n"; cout << toBinary( 8.9 + std::numeric_limits<double>::epsilon() ).to_string() << "\n"; cin.get(); } That epsilon is

binary representation of a float

℡╲_俬逩灬. 提交于 2019-12-08 04:05:25
问题 why this two call to toBinary function compute the same output (at least under VS2010) ? #include <iostream> #include <bitset> #include <limits> using namespace std; template<class T> bitset<sizeof(T)*CHAR_BIT> toBinary(const T num) { bitset<sizeof(T)*CHAR_BIT> mybits; const char * const p = reinterpret_cast<const char*>(&num); for (int i = sizeof(T)*CHAR_BIT-1 ; i >= 0 ; --i) mybits.set(i, (*(p)&(1<<i) )); return mybits; } int main() { cout << toBinary(8.9).to_string() << "\n"; cout <<

Conversion of entire array from int to double in order to do some aritmetic operations

谁说我不能喝 提交于 2019-12-08 03:45:12
问题 I have this bit of code: var arrayIntegers : [Int] = [] arrayIntegers += 0...33 var arrayDecimal : [Int] = [] arrayDecimal += 0...999 The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part. When I try to typecast this: var arrayDoubleComposed : [Double] = []

How to Shuffle 2d array in java

吃可爱长大的小学妹 提交于 2019-12-08 03:28:16
问题 What i want to do is shuffling the value of 2D array. I have this 2D array: 2.0|0.0|0.0|1.0|1.0|0.0|0.0|0.0|0.0|1.0|2.0|0.0|1.0|1.0|0.0| 0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|1.0|0.0|1.0|1.0|0.0|0.0|0.0| 1.0|1.0|1.0|0.0|0.0|1.0|0.0|1.0|0.0|0.0|1.0|0.0|0.0|0.0|0.0| and i want it shuffle to (eaxmple): 0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|1.0|0.0|1.0|1.0|0.0|0.0|0.0| 1.0|1.0|1.0|0.0|0.0|1.0|0.0|1.0|0.0|0.0|1.0|0.0|0.0|0.0|0.0| 2.0|0.0|0.0|1.0|1.0|0.0|0.0|0.0|0.0|1.0|2.0|0.0|1.0|1.0|0.0| how can i do that?

Convert float to double

断了今生、忘了曾经 提交于 2019-12-08 01:52:43
问题 I'm trying to convert Single to Double while maintaining the original value. I've found the following method: Single f = 5.2F; Double d1 = f; // 5.19999980926514 Double d2 = Double.Parse(f.ToString()); // 5.2 (correct) Is this practice recommendable? I don't need an optimal method, but the intended value must be passed on to the double. Are there even consequences to storing a rounded value in a double? 回答1: The conversion is exact. All the Single values can be represented by a Double value,