How to get float value as it is from the text box in objective c - objective-c

Can any one please help me how to get float value as it is from text box
for Ex: I have entered 40.7
rateField=[[rateField text] floatValue];
I am getting rateField value as 40.7000008 but I want 40.7 only.
please help me.
thanks in advance
Thanks Every body,
I tried all the possibilities but I am not able to get what I want. I am not looking to print the value to convert into string.I want to use that value for computation. If i use Number Formatter again when i am converting from number to float it is giving same problem.So i want float value only but it should be whatever i have given in the text box it should not be padded with any values.This is my requirement.Please help me.
thanks&regards Balu
Thanks Every body,
I tried all the possibilities but I am not able to get what I want. I am not looking to print the value to convert into string.I want to use that value for computation. If i use Number Formatter again when i am converting from number to float it is giving same problem.So i want float value only but it should be whatever i have given in the text box it should not be padded with any values.This is my requirement.Please help me.
thanks&regards
Balu

This is ok. There is not guaranteed that you will get 40.7 if you will use even double.
If you want to output 40.7 you can use %.1f or NSNumberFormatter

Try using a double instead. Usually solves that issue. Has to do with the storage precision.
double dbl = [rateField.text doubleValue];

When using floating point numbers, these things can happen because of the way the numbers are stored in binary format in the computers memory.
It's similar to the way 1/3 = 0.33333333333333... in decimal numbers.
The best way to deal with this is to use number formatters in the textbox that displays the value.

You are already resolved float value.
Floating point numbers have limited precision. Although it depends on
the system, float relative error due to rounding will be around 1.1e-8
Non elementary arithmetic operations may give larger errors, and, of
course, error progragation must be considered when several operations
are compounded.
Additionally, rational numbers that are exactly representable as
floating point numbers in base 10, like 0.1 or 0.7, do not have an
exact representation as floating point numbers in base 2, which is
used internally, no matter the size of the mantissa. Hence, they
cannot be converted into their internal binary counterparts without a
small loss of precision. This can lead to confusing results: for
example, floor((0.1+0.7)*10) will usually return 7 instead of the
expected 8, since the internal representation will be something like
7.9999999999999991118....
So if you're using those numbers for output, you should use some rounding mechanism, even for double values.

Related

Kotlin: Convert Hex String to signed integer via signed 2's complement?

Long story short, I am trying to convert strings of hex values to signed 2's complement integers. I was able to do this in a single line of code in Swift, but for some reason I can't find anything analogous in Kotlin. String.ToInt or String.ToUInt just give the straight base 16 to base 10 conversion. That works for some positive values, but not for any negative numbers.
How do I know I want the signed 2's complement? I've used this online converter and according to its output, what I want is the decimal from signed 2's complement, not the straight base 16 to base 10 conversion that's easy to do by hand.
So, "FFD6" should go to -42 (correct, confirmed in Swift and C#), and "002A" should convert to 42.
I would appreciate any help or even any leads on where to look. Because yes I've searched, I've googled the problem a bunch and, no I haven't found a good answer.
I actually tried writing my own code to do the signed 2's complement but so far it's not giving me the right answers and I'm pretty at a loss. I'd really hope for a built in command that does it instead; I feel like if other languages have that capability Kotlin should too.
For 2's complement, you need to know how big the type is.
Your examples of "FFD6" and "002A" both have 4 hex digits (i.e. 2 bytes).  That's the same size as a Kotlin Short.  So a simple solution in this case is to parse the hex to an Int and then convert that to a Short.  (You can't convert it directly to a Short, as that would give an out-of-range error for the negative numbers.)
"FFD6".toInt(16).toShort() // gives -42
"002A".toInt(16).toShort() // gives 42
(You can then convert back to an Int if needed.)
You could similarly handle 8-digit (4-byte) values as Ints, and 2-digit (1-byte) values as Bytes.
For other sizes, you'd need to do some bit operations.  Based on this answer for Java, if you have e.g. a 3-digit hex number, you can do:
("FD6".toInt(16) xor 0x800) - 0x800 // gives -42
(Here 0x800 is the three-digit number with the top bit (i.e. sign bit) set.  You'd use 0x80000 for a five-digit number, and so on.  Also, for 9–16 digits, you'd need to start with a Long instead of an Int.  And if you need >16 digits, it won't fit into a Long either, so you'd need an arbitrary-precision library that handled hex…)

Redshift ROUND function doesn't round in some cases?

I can find a workaround, but it is really annoying and I may certainly be missing something. Redshift's ROUND function doesn't round to the number of decimals specified.
For example,
select round(cast(176 as float)/cast(492 as float),4) as result;
Above select statement will return 0.35769999999999996.
However, this statement:
select round(cast(229 as float)/cast(491 as float),4) as result;
... will return 0.4664.
Why? I can work around this, but seems like it should work and return only four decimal places.
If your issues is all those 9999s, then the issue is floating point representation. Convert to a decimal to get fixed-point precision:
select round(cast(176 as float)/cast(492 as float), 4)::decimal(10, 4) as result;
Elaborating more on Gordon's answer -
So you’ve written some absurdly simple code, say for example:
0.1 + 0.2
and got a really unexpected result:
0.30000000000000004
Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.
When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.
What can I do to avoid this problem?
That depends on what kind of calculations you’re doing.
If you really need your results to add up exactly, especially when you work with money: use a decimal datatype.
If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
Shamelessly stolen from : Floating Point
try multiplying by 10 to the power of your desired places after the decimal point, rounding, and then dividing it out again:
-- exclude decimal point inside ROUND(), include outside ROUND()
SELECT ROUND(10000 * 176 / 492) / 10000.0
which will return the expected 0.3577.

What does %2.6f do in Objective C String Formatting?

Based on Apple's documentation here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html
It's pretty easy to understand the number to the right of the decimal point is the number of digis will be rounded up...
For example, %1.2f, 123456.123456 will turn out 123456.12 and %1.4f will turn out 123456.1234...
But it looks like the number to the left of decimal does nothing.
I tried changing the number to whatever I can think of, nothing happened.
What does it do?
The number before the decimal point in the format is called the format string's width. That is, if the resultant string would involves less characters than its width, it will be left-padded with blank spaces. You don't see any change because you either aren't using a high enough number (try something ridiculous like 100 or 200), or don't have a means of properly seeing your whitespace.

How to round up and return an integer value?

I know about the Math.Round and Math.Ceiling methods, but they return a Double and a Decimal. Does VB.NET have any built-in functions which always round a floating point number up, not down, with a return type of Integer? I know there's CInt, but this can round down if it's below 6.5.
From the comments I understood you wanted to round 6.1 to 7.
Just add 1 and truncate.
If it looks awkward, create a method for it.
Correction:
Unless the number already is truncated.
Addendum:
Note here that doing == with floats is not without problem; you should always have some sort of precision when trying == with floats.
Now when you have decided on this precision - then you can rewrite your code to add 0.999999 (according to precision) and the first add-0.9999-and-truncat works.
Note again that adding 0.9999 is does not really mean you add 0.9999 with our "normal" float.
So if you really want to add 0.9999 you have to work with BND and/or some monetary arithmetics.
Which you "always" should do when calculating money (or any exact decimal stuff)

How to convert a UTF8 string in a float value in objective-c

I here is the problem:
I have a NSString that contain "1.7" (for example) and I have to get the float number = 1.7
I' ve tried with [mystring floatValue] but the result is 1.700000000004576
If I try with "1.74" the result is 1.74000000000000000067484
how can I fix it?
thank you!
You are correctly converting the string into a float. The problem is that floating point numbers cannot represent all real numbers exactly. A direct assignment:
float x = 1.7;
will still have a precision error. That's just how floating point numbers are.
The workaround depends on your needs. Some examples: If you need more precision for mathematical calculations, you can use doubles. If you're trying to generate output for the user, you can format the output so it limits the number of digits shown after the decimal point. If you're dealing with money, you could convert floating point dollar amounts into integer numbers of cents and perform all calculations using integers, only showing a decimal point on output to the user.
Floats need to be able to represent infinitely many real numbers, but a float contains a finite number of bits, so floats are approximations.
See this article for more.
You can trim the answer by using the formatting below.
The .1 will set the result to one decimal place.
NSLog(#"mystring = %.1f ",[mystring floatValue]);
Solved!
I used sqlite3_bind_text even if my database attribute is FLOAT value and I used mystring instead of myfloat and it work fine! Thank you!