Swift - Convert string to double without scientific notation(exponents) - ios

I have a string which is in exponents. For example my string is
"-4.17182640083098e-31"
I have an array of values like this. I want to use this to draw chart.
The graph accepts double values only to plot it. When I convert it to double using Double(str) and using it in graph is not working because the graph library not accepting exponents.
So I converted this to decimal value the result is
-0.0000000000000000000000000000011803449813502
So I again converted this decimal to double it gives the same -4.17182640083098e-31 with exponents notation.
So can't use this is graph. So please suggest a way to convert this to double without the exponents/scientific notations. Thanks in advance.

Related

How to cast hex string as hex integer?

I'm using BLE plugin, which requires to present the values as list of hex number(e.g. [0x20,0x11,0x06,0x6D]).
I was able to get a list of hex strings using int.toRadixString (['0x20','0x11','0x06','0x6D']), but didn't find a way to convert those strings into hex integers.
I'm looking for something like int demicalToHex(int demical) or similar function.
Thanks in advance.
There's no such thing as a hex number. What it needs is a list of integers (List<int>).
In the same way that you have [0x20,0x11,0x06,0x6D] you could equally write that as [32, 17, 6, 109]. Sometimes, it's convenient to think about integers in their decimal representation or their hex representation (or octal, or binary...), but as far as the compiler is concerned, they are just integers.
Just use an array of integers (not strings).

Convert double 15,34 to 15.34

I'm trying to get a json string but I noticed that when I pass some double numbers to my query the integer part is separated by a coma from the floating part. I need them to be separated by a point. Is this connected to the Language of the os?
You can use a simple regex to fix this issue:
\d+,\d+ to select the offending bits of JSON, and a string-based replace on the results. If you already have the numbers separated out, use the replace function (something like val.replace(",",".")) on the value you have, and cast it to a float (float(val) in Python).

Converting Double to NSNumber in Swift Loses Accuracy [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
For some reason, certain Doubles in my Swift app are giving me trouble when converting to NSNumber, while some are not. My app needs to convert doubles with 2 decimal places (prices) to NSNumbers so they can be stored and retrieved using Core Data. For example, a few particular prices such as 79.99 would evaluate to 99.98999999999999 unless specifically formatted using NSNumber's doubleValue method.
Here selectedWarranty.price = 79.99 as shown in debugger
// item.price: NSNumber?
// selectedWarranty.price: Double?
item.price = NSNumber(double: selectedWarranty.price!)
I programmed some print statements to show how the conversion works out
Original double: 79.99
Converted to NSNumber: 79.98999999999999
.doubleValue Representation: 79.99
Can somebody explain if there is a reason why the initializer cannot surely keep 2 decimal places for every number? I would really like to store the prices in Core Data like they should be. Formatting every time it is displayed doesn't sound very convenient.
UPDATE:
Converted Core Data object to type NSDecimalNumber through data model, 79.99 and 99.99 no longer a problem, but now more manageable issue with different numbers...
Original double: 39.99
Converted to NSDecimalNumber: 39.99000000000001024
Firstly, you're confusing some terms. 79.98999999999999 is higher precision than 79.99 (it has a longer decimal expansion), but lower accuracy (it deviates from the true value).
Secondly, NSNumber does not store neither 79.99 nor 79.98999999999999. It stores the magnitude of the value according to the IEEE 754 standard. What you're seeing is likely the consequence of the printing logic that's applied to convert that magnitude into a human readable number. In any case, you should not be relying on Float or Double to store values with a fixed precision. By their very nature, they sacrifice precision in order to gain a longer range of representable values.
You would be much better off representing prices as an Int of cents, or as an NSDecimalNumber.
Please refer to Why not use Double or Float to represent currency?
That's how double works everywhere. If you need only 2 decimal places consider using integer/long instead adding point after second digit, when need to display the value.

Converting SHA1 hash Hex string to decimals using objective C on iPhone

I am a beginner to developing apps on the iPhone and I am trying to convert a 40 character SHA1 hash in hex to decimals. i have been looking around and the largest integer type in unsigned long long but it's not enough. I tried looking at NSDecimalNumber but it doesn't have Hex function.
Well I guess there is no way of doing so. I would have to change it to a string instead.

How to Remove exponent from formatted float in Delphi

Given a double value like 1.00500000274996E-8, how do I convert it to it's non scientific format with a maximum number of digits after the decimal point - in this case with 8 digits it would be 1.00500000?
The conversion should not pad with zeros, so 2007 would come out as 2007, and 2012.33 and 2012.33.
I've tried lots of combinations using Format, FormatFloat, FloatToStrF but can't quite seem to hit the jackpot. Many thanks for any help.
Edit: I should clarify that I am trying to convert it to a string representation, without the Exponent (E) part.
FormatFloat('0.######################', 1.00500000274996E-8) should do the trick.
Output is: 0,0000000100500000274996
It will not output more digits than absolutely necessary.
See John Herbster's Exact Float to String Routines in CodeCentral. Perhaps not exactly what youre after but might be good starting point... CC item's description:
This module includes
(a) functions for converting a floating binary point number to its *exact* decimal representation in an AnsiString;
(b) functions for parsing the floating point types into sign, exponent, and mantissa; and
(c) function for analyzing a extended float number into its type (zero, normal, infinity, etc.)
Its intended use is for trouble shooting problems with floating point numbers.
His DecimalRounding routines might be of intrest too.

Resources