Getting the first decimal place of a number - iphone

I have a number for example: 2.4444444. I need to get first digit after the dot -- in my case it's 4. How to implement it?

How about
( (int)(floor( fabs( num ) * 10 ) ) ) % 10

Chech these out! Tried it for you hope it helps
#include<stdio.h>
int main()
{
int b;
float a;
a=2.4444; 'load some value
b=(int)a; 'typecast it into integer you get 2 into b variable
a=a-b; ' subtract b from a and you will get decimal point value 0.4444
a=a*10; ' multiplying with 10 gives 4.444
b=(int)a; ' now apply the same logic again
printf("%d",b); 'outputs 4
}
Update written these function using these link
Extract decimal part from a floating point number in C

it could be as simple as
num*10%10

(int)(num * 10) - ((int)num) * 10

Related

Calc with format number

I am having a problem in formatting numbers. I am getting a string concatenation instead of the numerical sum.
I have attached the screenshot of the problem.
Help me please. Thanks.
https://drive.google.com/open?id=1vv5doL2QByU_54kPDoKFRvqj6aaSRxbg
https://drive.google.com/open?id=1-BktFl_j9H3tzKGysDoDlr4nB7cDVEGu
I need the sum of 5 + 5 + 5, but the result is 555 instead of 15.
Thanks
When you join the Number array it will be converted a string. Please update as below
var total = #All of Suma OD.reduce(function(a, b) { return a + b; }, 0);
total - [Your other calculation logics]

Postgresql: Calculating Percentages but casting doesn't work

I have following query where I am trying to do an arithmetic operation on columns. I have used the correct casting based on the postgres documentation but (http://www.postgresql.org/docs/8.3/static/datatype-numeric.html) but someone my bigint columns still cut of the results and avoid decimal points on the result sets.
select * from (
select (degree_one + degree_two) as degree_easy,
degree_three as degree_hard,
(((degree_one + degree_two)/(degree_one + degree_two + degree_three))::decimal) as easy_percent, ((degree_three/(degree_one + degree_two + degree_three))::decimal) as hard_percent from recommendation_degree
) as dc
where dc.degree_easy >= 4 and dc.degree_hard >= 4
What am I doing wrong here? In addition to decimal, I have tried float, real but both of them gives the same result.
You can try casting the dividend and divisor in the divisions to force the calculation to be done on decimal values, otherwise I think you'll end up doing integer division (which you then cast to decimal) and the result will be incorrect.
Try this:
select * from (
select (degree_one + degree_two) as degree_easy,
degree_three as degree_hard,
(degree_one::decimal + degree_two::decimal)/(degree_one::decimal + degree_two::decimal + degree_three::decimal) as easy_percent,
degree_three::decimal/(degree_one::decimal + degree_two::decimal + degree_three::decimal) as hard_percent
from recommendation_degree
) as dc
where dc.degree_easy >= 4 and dc.degree_hard >= 4
It might not be necessary to cast both dividend and divisor, it should work when only casting the divisor I think.
Sample SQL Fiddle

How to convert string to integer in JES

I am trying to do an assignment in JES a student jython program. I need to convert our student number taken as a string input variable to pass through our function i.e.
def assignment(stringID) and convert it into integers. The exact instructions are:
Step 1
Define an array called id which will store your 7 digit number as integers (the numbers you set in the array does not matter, it will be over written with your student number in the next step).
Step 2 Your student number has been passed in to your function as a String. You must separate the digits and assign them to your array id. This can do this manually line by line or using a loop. You will need to type cast each character from stringID to an integer before storing it in id.
I have tried so many different ways using the int and float functions but I am really stuck.
Thanks in advance!
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
I had to do some jython scripting for a websphere server. It must be a really old version of python it didn't have the ** operator or the len() function. I had to use an exception to find the end of a string.
Anyways I hope this saves someone else some time
def pow(x, y):
total = 1;
if (y > 0):
rng = y
else:
rng = -1 * y
print ("range", rng)
for itt in range (rng):
total *= x
if (y < 0):
total = 1.0 / float(total)
return total
#This will return an int if the percision restricts it from parsing decimal places
def parseNum(string, percision):
decIndex = string.index(".")
total = 0
print("decIndex: ", decIndex)
index = 0
string = string[0:decIndex] + string[decIndex + 1:]
try:
while string[index]:
if (ord(string[index]) >= ord("0") and ord(string[index]) <= ord("9")):
times = pow(10, decIndex - index - 1)
val = ord(string[index]) - ord("0")
print(times, " X ", val)
if (times < percision):
break
total += times * val
index += 1
except:
print "broke out"
return total
Warning! - make sure the string is a number. The function will not fail but you will get strange and almost assuredly, useless output.

High-precision random numbers on iOS

I have been trying this for a while but thus far haven't had any luck.
What is the easiest way to retrieve a random number between two very precise numbers on iOS?
For example, I want a random number between 41.37783830549337 and 41.377730629131634, how would I accomplish this?
Thank you so much in advance!
Edit: I tried this:
double min = 41.37783830549337;
double max = 41.377730629131634;
double test = ((double)rand() / RAND_MAX) * (max - min) + min;
NSLog(#"Min:%lf, max:%lf, result:%lf",min,max,test);
But the results weren't quite as precise as I was hoping, and ended up like this::
Min:41.377838, max:41.377731, result:41.377838
You can normalise the output of rand to any range you want:
((double)rand() / RAND_MAX) * (max - min) + min
[Note: This is pure C, I'm assuming it works equivalently in Obj-C.]
[Note 2: Replace double with the data-type of your choice as appropriate.]
[Note 3: Replace rand with the random-number source of your choice as appropriate.]

binary to decimal in objective-c

I want to convert the decimal number 27 into binary such a way that , first the digit 2 is converted and its binary value is placed in an array and then the digit 7 is converted and its binary number is placed in that array. what should I do?
thanks in advance
That's called binary-coded decimal. It's easiest to work right-to-left. Take the value modulo 10 (% operator in C/C++/ObjC) and put it in the array. Then integer-divide the value by 10 (/ operator in C/C++/ObjC). Continue until your value is zero. Then reverse the array if you need most-significant digit first.
If I understand your question correctly, you want to go from 27 to an array that looks like {0010, 0111}.
If you understand how base systems work (specifically the decimal system), this should be simple.
First, you find the remainder of your number when divided by 10. Your number 27 in this case would result with 7.
Then you integer divide your number by 10 and store it back in that variable. Your number 27 would result in 2.
How many times do you do this?
You do this until you have no more digits.
How many digits can you have?
Well, if you think about the number 100, it has 3 digits because the number needs to remember that one 10^2 exists in the number. On the other hand, 99 does not.
The answer to the previous question is 1 + floor of Log base 10 of the input number.
Log of 100 is 2, plus 1 is 3, which equals number of digits.
Log of 99 is a little less than 2, but flooring it is 1, plus 1 is 2.
In java it is like this:
int input = 27;
int number = 0;
int numDigits = Math.floor(Log(10, input)) + 1;
int[] digitArray = new int [numDigits];
for (int i = 0; i < numDigits; i++) {
number = input % 10;
digitArray[numDigits - i - 1] = number;
input = input / 10;
}
return digitArray;
Java doesn't have a Log function that is portable for any base (it has it for base e), but it is trivial to make a function for it.
double Log( double base, double value ) {
return Math.log(value)/Math.log(base);
}
Good luck.