Linked Questions

0 votes
0 answers
437 views

Swift : impossible to round a double correctly [duplicate]

I would like to round a double with 2 decimals. I have tried many ways - round * 100 /100 - by passing with a string but it's the same matter I would like 166.67 but I get 166.66999999 (look at ...
FREDERIC1405's user avatar
1 vote
0 answers
128 views

Initial unprecise float value [duplicate]

Can someone explain this behaviour to me? Why is the float value initially not as precise as I would expect? Why is the double cast creating an even less precise value?
Bruno's user avatar
  • 212
0 votes
0 answers
68 views

Swift 2.2 : Decimal conversion not working as expected in Xcode [duplicate]

Trying to convert string to decimal and shows unexpected values for some strings such as 8.80 and 9.80 , tried to run some code in playground and Xcode, please refer below screen shots. I suspect this ...
Max's user avatar
  • 5,992
0 votes
1 answer
48 views

Issue in converting string to double [duplicate]

I have mobile bank app. When user type amount, then convert string to double i have problem user typed amount example "8.7" is 8.699999999999999 and when i send request it sending 8.699999999999999 ...
Tornike Davitashvili's user avatar
0 votes
0 answers
23 views

Same operation but totally different digit precision in Swift [duplicate]

I'v tried following code when learning the mutation function of structure type struct SelfMutaPoint { var x = 0.0, y = 0.0 mutating func moveBy(x deltaX: Double, y deltaY: Double) { ...
SLN's user avatar
  • 5,142
4025 votes
36 answers
616k views

Is floating-point math broken?

Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004 Why do these inaccuracies happen?
Cato Johnston's user avatar
1 vote
3 answers
3k views

Round Issue in swift

I have this Info. let params2: [String: AnyObject] = [ "app_token": myapptoken, "member_access_token": accessToken!, "pay_process": 0, "payamount_credit": 9.87 //hardcode ] When print ...
jose920405's user avatar
  • 8,065
2 votes
2 answers
4k views

Convert JSON data to Double in Swift

I receive the following JSON response: { "id": 1, "value": 519.6365 } But when I convert the value to Double, I get: 519.63649999999996 How to keep the original value? (without rounding it)
Manu's user avatar
  • 23
8 votes
1 answer
1k views

How to keep original double value while parsing in json swift?

I received the following response from server "transactionInfo": { "currencyCode": "MYR", "total": 472.23, "roomCurrencyCode": "MYR", "roomTotal": 472.23 } but when I am ...
Shiv Jaiswal's user avatar
0 votes
1 answer
177 views

How to prevent currency with decimal rounds in Swift

Seems to be trivial but I couldn't figure out how to prevent the currency value from Rounding in Swift. Below is my code: let halfYearlyPrice = 71.99 var perMonthPrice = (halfYearlyPrice as Double) / ...
Mahendra Liya's user avatar
1 vote
1 answer
124 views

Why this happens in floating point conversion?

I noticed that some floating points converted differently. This question helps me about floating points however still don't know why this happens? I added two screenshots from debug mode about the ...
yilmazburk's user avatar
0 votes
1 answer
90 views

In Swift 4, what is the correct way of typecasting a String value to a Float value?

I came across a situation where the following code does not work as expected in a project whereas works fine in Playground. strtof("0.9", nil) //expected to return 0.9 Float("0.9")! //expected to ...
Rikesh Subedi's user avatar