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?).
Related
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.
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.
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));
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I'm new to swift (using swift 2.2) and I'm seeing an issue where 200.23 - 200 is shown to be equal to 0.2299999999999898. I know that computers can't represent irrational numbers accurately, but I don't understand why a rational number is also not being represented correctly. This is affecting the output numbers generated by my program, leading to issues.
Here's a sample playground code (a very simple version of part of my actual code) showing this behavior:
var number:Double = 200.23 //Number I'm testing
var left: Double = floor(number) // Extracting integer part
var right: Double = number-left // Extracting fraction part
In the above code I'm defining a number and then extracting its whole and fractional parts. The fractional part should be 0.23, however it's shown as 0.2299999999999898 in swift's playground.
Similarly, if I change the original number from 200.23 to 100.23, then the fractional part is shown to be 0.230000000000004
How can I solve this issue ?
EDIT:
I've been asked about the context of this problem. I'm designing a statistics app that uses numbers a lot and actually has a small builtin calculator too. So if I user tries to calculate 200.23 - 0.23 and sees anything other than 0.23, he's gonna be surprised!
To round the values stored in the variable right just use the round() function. To specify the precision just do this
var number:Double = 200.23
var left: Double = floor(number)
var right: Double = number-left
let roundedValue = round(right*100)/100 // Answer will be 0.23
I know that swift's Double values have 15 decimal point precision so I took a variable
let pi: Double = 3.1415926535897932384
and REPL returned me
pi: Double = 3.1415926535897931
One thing I can clearly see that REPL has rounded off 32384 to 31(in case of overflow). So, is it following the standard mathematics rule for rounding off or something else.
This behavior has to do how floating point digits are represented in binary. So the conversion to binary doesn't round to the next decimal representation instead it converts it to the next binary one.
// test this in a playground
9.05 // returns 9.050000000000001
You shouldn't consider the last digit of a double value in general.