iphone - converting float to integer - iphone

This is a simple question:
Is this a correct way to get an integer part from a float division?
int result = myFloat / anInteger;
this is working, but I am not sure if it is the best way.
thanks for any help.

To truncate:
int result = (int)(myFloat / anInteger);
Other conversion options:
#include <math.h>
int result = (int)ceilf(myFloat / anInteger);
int result = (int)roundf(myFloat / anInteger);
int result = (int)floorf(myFloat / anInteger);

Related

how to convert this result of calculation flutter

wanna know how to show this result like int number
var symbol = data[index]["symbol"];
double cureentprice = datarates["price"]["$symbol"];////this double is 1.17150
double enrty = double.parse(data[index]["entryprice"]);////this is 1.17100
double lo = cureentprice - enrty ;//i got like this result 0.00050
as you see above i got the result 0.00050 but i need it like this 50
any idea to do somthing like that??
Try This. Maybe This should Help. If this doesn't work then let me know.
var symbol = data[index]["symbol"];
double cureentprice = datarates["price"]["$symbol"];////this double is 1.17150
double enrty = double.parse(data[index]["entryprice"]);////this is 1.17100
// double lo = cureentprice - enrty ;//i got like this result 0.00050
int scale = 100000;
double lo = currentPrice - enrty;
var value1 = (lo * scale).floor();
var value2 = (lo * scale).ceil();
print(value1);
print(value2);
You can first scale and then round-off using floor() or ceil() function to get desired output.
double currentPrice = 1.17150;
double enrty = 1.17100;
int scale = 100000;
double lo = currentPrice - enrty;
print((lo * scale).floor()); //prints 49
print((lo * scale).ceil()); //prints 50

iOS Swift: Create Float value from two Ints

What's the best way to create a single Float value from two Ints? I have two vars:
let int1 = 165
let int2 = 5
I'm trying to combine them into a Float with the value 165.5.
Float to Int
Two approaches.
You can concatenate them into a String and pass that to a Float initializer:
let float1 = Float("\(int1).\(int2)")
Or you can divide int2 by 10 and add int1:
let float2 = Float(int1) + Float(int2)/10
Int to Float
If you want to go back, you can again use strings:
let float : Float = 165.5
let intArray = String(float)
.characters
.split(".")
.map { Int(String($0))! }
intArray[0] // 165
intArray[1] // 5
But it's simpler to use math:
let (int1, int2) = (Int(float), (float - floor(float)) * 10)

Cast an int to a Float in Swift

Im trying to cast the results of a calculation (ShotPercentage) to a Float and present the results in the App as a 89 percent for example. But I am struggling with the type casting any help would be greatly appreciated. Here is my code:
// calculate shot percentage
shotPercentage = makeCounter / totalCounter
shotPercentageLabel.text = "\(shotPercentage)"
You can apply a conversion to your Floatresult like this.
shotPercentage = Int(makeCounter / totalCounter)
var shotPercentageInt: Int
shotPercentageInt = 3/5
println("\(shotPercentageInt*100)%") // 0% because 3/5 = 0.6 -> Int = 0
//
var shotPercentageFloat: Float
shotPercentageFloat = 3/5
println("\(shotPercentageFloat*100)%") // 60.0% because 3/5 = 0.6 -> Float = 60%
// Convert Float to Int
var shotPercentageFloatToInt: Int
shotPercentageFloatToInt = Int(shotPercentageFloat)
println("\(shotPercentageFloat)") // 0.6
// It's amazing
You might want to use NSNumberFormatter. It does well going to strings and coming from strings. Regardless, it would look like:
let makeCounter = 15
let totalCounter = 20
let shotPercentage:Float = makeCounter / totalCounter
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
if let percentString = formatter.stringFromNumber(shotPercentage) {
shotPercentageLabel.text = percentString
}
In this case the label would read 75%, as the formatter will include the percent sign for you.

Java math issue

Can any one tell me how to write this type of code in eclipse propject. basically i want to square root the samv and samvi variables. i am getting syntax error.
int ki, l,;
int samv = (tdrum / k) / l;
int samvi = (tdrum / ki) / l;
int samv2 = (Math.sqrt(samv);
int samv2i = (Math.sqrt(samvi);
Many Error extra commas less brackets
int ki, l;
int samv = (tdrum / k) / l;
int samvi = (tdrum / ki) / l;
int samv2 = (Math.sqrt(samv));
int samv2i = (Math.sqrt(samvi));
Take care of the brackets. And use a double because sqrt() returns no int:
double samv2 = Math.sqrt(samv);
double samv2i = Math.sqrt(samvi);

float + float = NAN

float jk = 7700; float ck = 8000; - if i do int jk; I get rim=0;
printf (asin(jk/10500)) = 1.57897 - for example
printf (asin(ck/9500)) = 0.87868 - for example
float rim;
rim= asin(jk/10500)+ asin(ck/9500);
printf("\n%f", rim) = nan
Why i get nan?
I don't believe your "for example". Because I don't believe in magic. If you have two valid floats both pretty small, then their sum is not a nan. So, my guess is this:
either |jk| > 10500 or |ck| > 9500. So you make asin with an invalid ( > 1.0 or < -1.0) argument and thus get a nan.
Or you have made another error. Please post a compilable runnable example which will print NAN
There's either something wrong with your code or something seriously wrong with the iphone. The following code:
#include<stdio.h>
#include<math.h>
int main (void) {
printf ("%f\n", asin(1));
printf ("%f\n", asin(0.5));
float rim;
rim = asin(1) + asin (0.5);
printf ("%f\n", rim);
return 0;
}
produces a more sensible:
1.570796
0.523599
2.094395
In other words, both your asin(0.5) and your sum are incorrect.
Are you sure that you didn't actually do something like:
rim = asin(1 + asin (0.5));
That will indeed give you NaN.
Update based on your added info:
Your code still works fine in my environment:
#include<stdio.h>
#include<math.h>
int main (void) {
float jk = 7700;
//jk = 7700/10500;
jk = jk/10500;
printf ("%f\n", asin(jk));
float hg = 8000;
hg = hg / 9500;
printf ("%f\n", asin(hg));
float rim;
rim = asin(jk) + asin (hg);
printf ("%f\n", rim);
return 0;
}
outputting:
0.823212
1.001175
1.824387
You'll notice I changed jk = 7700/10500 to jk = jk/10500 since the latter gave you 0 due to the integer division, but I don't get NaN in either case. You really need to post a complete program which shows the errant behaviour.
#include <stdio.h>
#include <math.h>
main()
{
float jk=7700, ck=8000;
printf ("\n%f",asin(jk/10500));
printf ("\n%f",asin(ck/9500));
float rim;
rim= asin(jk/10500)+ asin(ck/9500);
printf("\n%f", rim);// = nan
}
Output
0.823212
1.001175
1.824387
I think your code is right. The problem is the value of jk and ck.
You kown if |jk/temp|>1 or |ck/temp|>1, the return of asin(jk/temp) will be nan.
so try to make |jk/temp|<=1 and |ck/temp| <=1, I believe that the return will be ok.