Can't convert and use string value in Double - Swift [duplicate] - swift

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have a easy problem but I can't fix it. In my program There are lines where I have a for loop with custom array. This array has got 3 value each of them string(array created by the JSON object from web). Whenever I tried the reach these string variables in double form I can't as pictures show.
As you can see in the pictures my aim is get he latitude and longitude values turn into double and use in CLLocationCoordinate2D init. but the conversion of string value is for latitude = 35.32041000000002 how can I convert it into presicion 6 digit. Thanks all. Have a nice day.

Read this to understand floating point numbers. In summary: Not all numbers can be exactly represented in the 64-bits of a Double. So you'll end up with something that extremely close, but with extra digit(s), or for example with a long tail of 9999999999.
The conversion succeeded. The value that the extra digit (0.000000000000002) won't hurt as they are rather insignificant.

Related

Why is 500/1000 is printing 0? [duplicate]

This question already has answers here:
Math divison in Swift
(4 answers)
Is the Swift divide "/" operator not working or have I missed something?
(3 answers)
Closed 1 year ago.
I'm trying to understand why print(500/1000) print 0 ?
It should be 0.5 but it's not printing it?!
This happed to me when I started to show a progress bar based on total value and current value but I came across this issue and now I'm stuck
You are working with whole numbers, so it's returning a whole number.
If you use decimals, it should divide as expected.
I am not really in Swift, but I assume that in this language 500 and 1000 are both integer values (like in other programming languages).
Then 500/1000 is 0, with a remainder of 500.
If you want to have a fractional result, you should consider to use the proper datatype (I don't know what this is in Swift ... something like double or float, I assume): write 500.0/1000.0.

Create NSDecimalNumber or Decimal from a Double in Swift [duplicate]

This question already has answers here:
How to store 1.66 in NSDecimalNumber
(1 answer)
Rounding a double value to x number of decimal places in swift
(34 answers)
Closed 4 years ago.
Trying to create a Decimal (or NSDecimalNumber) out of the given Double value for MONEY calculations within my app. I am using Swift 4 in Xcode 10.
But it generates following values which are wrong.
let doubleVal: Double = 56.203
Decimal(doubleVal) // Wrong Answer: 56.20299999999998976
Decimal.init(doubleVal) // Again Wrong Answer: 56.20299999999998976
If I use the String initialization, that works correct.
Decimal(string: "56.203")! // Correct Decimal: 56.203
I remember had the same problem in java when work with BigDecimal if call constructor with a double value. E.g. new BigDecimal(double val)... So they have this extra method valueOf BigDecimal.valueOf(amount); that creates correct BigDecimal out of the double.
How do I generate a Decimal out of a given Double value with Swift?
When Swift is converting a base 10 decimal number (56.203) to a binary number, there will always be some floating point error. As you have stated that you are using these for money calculation, Int is recommended (maybe multiply by 1000 when showing to users?).

why ncread function in matlab reduce floating point numbers count? [duplicate]

This question already has answers here:
How to store more than 4 decimal places in an array in MATLAB
(2 answers)
Closed 5 years ago.
i have code below in matlab:
converted = ncread(this_file, 'U');
disp(converted(50,10,20));
and the result is:
-0.1561
actually the number is -0.15617890 but this code changes the the number of floating numbers. why?
MATLAB displays only 4 digits after the decimal point by default. You can use format to display more digits:
format long
converted = ncread(this_file, 'U');
disp(converted(50,10,20));

why I cannot get just a number [duplicate]

This question already has answers here:
Convert output from symbolic math (sym) to float
(2 answers)
Closed 8 years ago.
I did following:
clc
clear all
I0=1.2e12;
FWHM=10e-12;
c=FWHM./2.35482;
t=0:1e-12:50e-12;
syms t
int(I0.*exp(-1.*(t-5e-12).^2./(2.*c.^2)),t,0,40e-12)
but it does not give me a simple number (just a number)
The reason why matlab does not automatically give you a number is that precision could be lost.
Suppose you have a symbolic variable with value 1/3. That has infinite accuracy at this point. Yet if you evaluate it, you would lose this precision, so that is why it is not evaluated directly.
If you want to evaluate it, you could do that of course. Try doc double,doc vpa, doc eval or doc subs. I think the first one is what you need.

Matlab number formatting?

I'm having some difficulties processing some numbers. The results I get are some like:
0.000093145+1.6437e-011i
0.00009235+4.5068e-009i
I've already try to use format long and as alternative passing to string and then str2num and with no good results also. Although is not being possible to convert them properly as I want (e.g. to a number with 9 decimals) If nobody is able to help me, at least I would appreciate if someone can tell me how to interpret the meaning of the i base.
You are talking about the imaginary unit i. If you are just using real number, you could neglect the imaginary part (it is very small). Thus, try:
real(0.000093145+1.6437e-011i)
After taking real() you can also control the decimal place formatting by sprintf:
sprintf('%0.2f', pi)
Will result in:
'3.14'
Place a 9 instead of a 2 for 9 decimal places.