How can i get the last digit of an integer (or NSInteger) outputted to integer?
example:
int time = CFAbsoluteGetCurrent();
int lastDigit;
Use modulo:
int lastDigit = time % 10;
Related
I want to divide two Integers and get a BigDecimal back in Kotlin.
E.g. 3/6 = 0.500000.
I've tried some solutions, like:
val num = BigDecimal(3.div(6))
println("%.6f".format(num))
// The result is: 0.000000
but none of them solve my problem.
3 and 6 are both Int, and dividing one Int by another gives an Int: that's why you get back 0. To get a non-integer value you need to get the result of the division to be a non-integer value. One way to do this is convert the Int to something else before dividing it, e.g.:
val num = 3.toDouble() / 6
num will now be a Double with a value of 0.5, which you can format as a string as you wish.
You might have better luck with:
val num = 3.toBigDecimal().divide(6.toBigDecimal())
println(num)
// prints 0.5
You have to convert both numbers to BigDecimal for the method to work. This will show the exact quotient, or throw an exception if the exact quotient cannot be represented (ie a non-terminating decimal).
You can set the scale and rounding mode as follows:
val num = 3.toBigDecimal().divide(6.toBigDecimal(), 4, RoundingMode.HALF_UP)
println(num)
// prints 0.5000
Link to reference article
Dividing Int by Int will give Int result only. To get float result , you need to convert one of the number to float.
You can use toFloat() function also.
var result = Int.toFloat() / Int
I want to convert a decimal number to an int without rounding it, for example:
if the number is 3.9 it will be turned into 3 (if it would have been rounded it would be 4).
You generally don't need to do anything special, as by default a cast from float/double to an integer type results in truncation:
float f = 3.9f;
int i = (int)f; // i = 3
It depends on how you want the negative values be treated. Typecasting to int would just truncate in that way that the part left of the decimal point will remain. -3.9f would turn into -3. Using floor before casting would ensure that it results in -4.
(all within the variable type boundaries of course)
you can do like this bellow :-
float myFloat = 3.9;
int result1 = (int)ceilf(myFloat );
NSLog(#"%d",result1);
int result2 = (int)roundf(myFloat );
NSLog(#"%d",result2);
int result3 = (int)floor(myFloat);
NSLog(#"%d",result3);
int result4 = (int) (myFloat);
NSLog(#"%d",result4);
OUTPUT IS
4
4
3
3
Just cast the float to an int and it will truncate your result.
You can simple typecast to int
I have a variable which contain the textfield value. I want to multiply that x value with a decimal number like 0.013. But after multiplication I got as answer 0.
It takes the decimal value as 0. What is the reason?
Once you get the text from the textfield, convert the string using floatValue :
CGFloat val = [myValue floatValue];
CGFloat res = val * 0.013;
In a comment, the OP notes
If i use 4/3 then it only takes answer as 1
This suggests that the problem is in how the value is initialized: 4/3 is integer division, returning the int value 1. The solution is to be sure that the calculations are actually dealing with floats, start to finish, by using float literals, e.g., replacing 4/3 with 4.0/3.0
you need to convert the text to the required format:
float floatValue = [yourTextField floatValue];
int intValue = [yourTextField intValuew];
I'm using the standard equation of distance / speed = arrival time. This works fine, but the answer is a float number and most people would find it awkward to convert something like 1.75 hrs to be 1 hr and 45 minutes.
I want to take that final float number result and extract the hour(s) separately from the minutes as integers.
Here is what I've tried:
-(IBAction)calculate:(id)sender {
float spd=[speed.text floatValue];
float dist=[distKnots.text floatValue];
//this give me the answer as a float
float arr=(dist/bs);
//this is how I showed it as an answer
//Here I need to convert "arr" and extract the hours & minutes as whole integers
arrivalTime.text=[NSString stringWithFormat:#"%0.02f", arr];
[speed resignFirstResponder];
}
And this is the conversion I tried to do -- and on paper it works, but in code it's full of errors:
int justHours = arr*60;
int justMinutes = (arr*60)-(justHours*60);
//then for the user friendly answer:
arrivalTime.text=[NSString stringWithFormat:#"%n hours and %n minutes", justHours, justMinutes];
I'm new to Objective-C and hoping there is a way to get this to work or a better way altogether to resolve this.
Your arr variable is already measured in hours, so you shouldn't be scaling it, just rounding it down:
int justHours = (int)arr;
and then your minutes is sixty times the (integer) difference between the original and rounded hours (i.e. the fractional part).
int justMinutes = (int)((arr - justHours) * 60);
The int justHours = arr/60; seems to be incorrect, it should be int justHours = arr;.
check the NSNumber numberFormater class. I believe you can wrap your float with time format and the return it to the user.
How can I concatenate two numbers like 7 and 6 to result in the number 76, or 3 and 3 so the result is 33, in objective-c?
There is no built in symbol to concatenate numbers. However, you can accomplish this by doing:
int first; /* Assuming this is initialized to the first number */
int second; /* Assuming this is initalized to the second number */
int myVal = [[NSString stringWithFormat:#"%d%d",first, second] intValue];
FirstNum * 10 + secondNum :-)
That's not a numeric operation, it's string concatenation.
Shortcuts in Objective-C to concatenate NSStrings
If you want two numbers x and y to add to xy, you can do
10*x + y.
For 7 and 6
7*10 + 6 = 76
I don't know much about objective-c but I would say:
If you get the numbers from an array, like nums= array(7,6), initialize result= 0 and then do a foreach on them. For each value you find, do : res= res*10 + value. At the end, even if you got 7 numbers to concatenate you'll get the result right. ie:
Array nums= Array(7,6,8,9);
int res= 0;
int value;
foreach (value in nums)
res= res*10 + value;
If you can use strings, just concatenate them like suggested above. there is probably a function to concatenate all values from an array as well to make it flexible.
Hope it helps
C^