Initial unprecise float value [duplicate] - swift

This question already has answers here:
Is floating point math broken?
(31 answers)
swift: issue in converting string to double
(2 answers)
Closed 4 years ago.
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?

Related

Converting int to string with leading zeros in postgresql [duplicate]

This question already has answers here:
Padding zeros to the left in postgreSQL
(4 answers)
Closed 4 months ago.
For example i have ints 12 and 7
I want to convert this numers to 5-char string with leading zeros, so 12 -> 00012, 7->00007.
It is possible to do it in postgres?
Ok, I find the answer
SELECT TO_CHAR(7,'fm00000');
SELECT TO_CHAR(12,'fm00000');

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.

How can i print 5 digit after floating point in Swift 4, is there any shortest way? [duplicate]

This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 4 years ago.
I want to print 5 digit after point in Swift 4. In C language i can do it as .5f but now i can't. Have any shortest code to do this?
String(format: "%.5f", 1.831831)

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));

Swift : impossible to round a double correctly [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
swift: issue in converting string to double
(2 answers)
Closed 6 years ago.
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 the picture)