double to int and comparison - iphone

I have a textfield and want to get the value stored as double and also compare whether it is between 2 and 20
double numOfYears = [[numOfYearsFld text] doubleValue];
Please let me know if the code above is correct and how can i compare/check between 2 and 20, since it is double ?

Yes, while your text field contains valid number your code to get its double value is OK (if field does not contain valid number your code will return 0).
Checking if your value is between 2 and 20 does not differ from what you would do in case you had integer (or any other plain number type):
if (numOfYears >= 2 && numOfYears <=20){
// check passed
}

Related

how to convert negative int to positive int in flutter

for example if i have a list like
List list = [-12,3,-24,58,12];
i have already tried this ,i did not understand it https://stackoverflow.com/questions/55374813/why-abs-function-in-dart-return-negative-number-when-not-wrapped-in-parenthesi\
i want to convert all parameters to a positive parameters
or if i had only a integer which is negative how to convert it to a positive integer
if you need more information please let me know and thanks for the helps
You can use Abs method for numbers to get absolute value:
int number = -5;
print(number.abs()); // prints: 5
https://dart.dev/guides/language/language-tour#numbers

Convert Double To Long With Checks

As you can see by the code below it should if out of range return the right long value without getting a random number when casting. However this code doesn't execute for the minimum value at all?
System.out.println(Double.MIN_VALUE < Long.MAX_VALUE);//this prints true so it should at the min value be in range meaning normal casting should work
public static long castLong(double d)
{
if(d > Long.MAX_VALUE)
return Long.MAX_VALUE;
else if(d < Long.MIN_VALUE)
{
System.out.println("here");
return Long.MIN_VALUE;
}
return (long)d;
}
based on the code above it should cast the issue is even when the values are in range it doesn't cast properly just goes to 0. So What am I suppose to do for said function make a double/float to string then how do I convert the string into long since it prints "4.9E-324". So what for the E fill with zeros? or what am I suppose to do to convert it in range of long max/min value when converting?

converting an int to a string in Objective-C while preserving '0'

I am trying to convert an int to a string in objective-C.
I read the other questions on SO about converting ints to strings, and I tried this method in my code:
-(void)setCounter:(int)count
{
counterText.text = [NSString stringWithFormat:#"%d",count];
}
However, if I want to display a number like '01' the 0 is taken out of the conversion and only '1' is displayed. Is there a workaround?
There is no such number as 01. If you write
int count = 01;
it is compiled equivalently to
int count = 1;
In fact, be careful: 07 is equivalent to 7, but 011 is equivalent to 9!
What you can do is ask stringWithFormat: to give you the zero-padding:
[NSString stringWithFormat:#"%02d",count]
should give you "02" if count is 2. To deconstruct it:
% - interpolate the next value here
0 - pad it to the width by placing zeroes on the left side
2 - width is 2 characters
d - it will be an integer. Do it now.
If you want a different format to the one shown, use it:
counterText.text = [NSString stringWithFormat:#"%02d",count];
There are a huge range of possibilities with the format string.
if any number start from 0 then Its a octal representation (0 - 7). you can add zero explictly using below line.
counterText.text = [NSString stringWithFormat:#"0%d",count];
Check out..
NSNumber +numberWithInt
and then:
NSNumberFormatter -setMinimumIntegerDigits
and then get the string representation with:
NSString -stringFromNumber

Performing operations on a double returns 0

I have a method that receives a number in a NSString format.
I wish to convert this string to a double which I can use to calculate a temperature.
Here's my method.
NSString *stringTemp = text; // text is a NSString
NSLog(#"%#",stringTemp); // used for debugging
double tempDouble = [stringTemp doubleValue];
NSLog(#"%f",tempDouble); // used for debugging
Please note I put the NSLog commands here just to see if the number was correct. The latter NSLog returns a value of 82.000000 etc. (constantly changes as it's a temperature).
Next I wanted to use this double and convert it to a Celsius value. To do so, I did this:
double celsiusTemp = (5 / 9) * (tempDouble - 32);
Doing this: NSLog(#"%d", celsiusTemp); , or this: NSLog(#"%f", celsiusTemp); both give me a value of 0 in the console. Is there any reason why this would be happening? Have I made a stupid mistake somewhere?
Thank you for your help!
Try doing (5.0 / 9.0). If you only use an int to do math where you are expecting a double to be returned (like 0.55) everything after the decimal place will be lost because the cpu expects an int to be returned.
5 / 9 is the division of two integers, and as such uses integer division, which performs the division normally and then truncates the result. So the result of 5 / 9 is always the integer 0.
Try:
double celsiusTemp = (5.0 / 9) * (tempDouble - 32);
If you evaulate (5/9) as an integer, then it is just 0.

NSDecimalNumber and popping digits off of the end

I'm not quite sure what to call it, but I have a text field to hold a currency value, so I'm storing that as a NSDecimalNumber. I don't want to use the numbers & symbols keyboard so I'm using a number pad, and inferring the location of a decimal place like ATMs do. It works fine for entering numbers. Type 1234 and it displays $12.34 but now I need to implement back space. So assuming $12.34 is entered hitting back space would show $1.23. I'm not quite sure how to do this with a decimal number. With an int you would just divide by 10 to remove the right most digit, but that obviously doesn't work here. I could do it by some messy converting to int / 10 then back to decimal but that just sounds horrific... Any suggestions?
Call - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(id < NSDecimalNumberBehaviors >)behavior on it
How about using stringValue?
1) NSDecimalNumber to String
2) substring last
3) String to NSDecimalNumber
Below is an example for Swift 3
func popLastNumber(of number: NSDecimalNumber) -> NSDecimalNumber {
let stringFromNumber = number.stringValue //NSNumber property
let lastIndex = stringFromNumber.endIndex
let targetIndex = stringFromNumber.index(before: lastIndex)
let removed = stringFromNumber.substring(to: targetIndex)
return NSDecimalNumber(string: removed)
}
If your input number is a single digit, it would return NaN.
You could replace it to NSDecimalNumber.zero if you need.
It may works like delete button on calcultor.
It's not tested much.
If someone found another NaN case, please report by reply.