iPhone time comparison - iphone

I am trying to figure out how to create an 'if' statement that uses a time value as a condition. For example:
if (time <= 10:00) {
score = 3;
} else if (time <= 20:00) {
score = 5;
} else {
score = 9;
}
I know that a string of "5:23" cannot be compared this way but I don't think I can just turn a string value like that directly into an integer. Any thoughts?

Try this:
NSString *realTimeString = #"10:00";
NSString *someTimeString = [realTimeString stringByReplacingOccurrencesOfString:#":"
withString:#"."];
float time = [someTimeString floatValue];
if (time <= 10.00) {
score = 3;
} else if (time <= 20.00) {
score = 5;
} else {
score = 9;
}

Related

error: expected '} before else

I am using eclipse and I will ask a stupid question: why the error? code should be ok. I guess I'm just getting old. I just don't see the error. all the {} match. What the code does it sets the hemisphere for geolocation in a flight simulator, as the math should not have negative numbers.
so here goes:
O_lat is tested for zero or a positive number
if positive don't mess with it. and make quadrant = 1
else its negative so we make it positive and make the quadrant = 0
that's it.
int vns1, vew1, vns2, vew2;
if( O_lat >= 0 ){
{ vns1 = 1; }
else {
vns1 = 0;
O_lat = O_lat * -1; }}
All the braces match, but the else is not where it should be for the if.
Remove one of the superfluous braces:
int vns1, vew1, vns2, vew2;
if( O_lat >= 0 ){
vns1 = 1;
}
else {
vns1 = 0;
O_lat = O_lat * -1;
}
Just re-arrange the code a bit:
int vns1, vew1, vns2, vew2;
if( O_lat >= 0 )
{
{ //this is unnecessary
vns1 = 1;
}
else
{
vns1 = 0;
O_lat = O_lat * -1;
}
}//this is unnecessary too

Calculate IRR (Internal Rate Return) and NPV programmatically in Objective-C

I am developing a financial app and require IRR (in-built functionality of Excel) calculation and found such great tutorials in C here and such answer in C# here.
I implemented code of the C language above, but it gives a perfect result when IRR is in positive. It is not returning a negative value when it should be. Whereas in Excel =IRR(values,guessrate) returns negative IRR as well for some values.
I have referred to code in above C# link too, and it seems that it follows good procedures and returns errors and also hope that it returns negative IRR too, the same as Excel. But I am not familiar with C#, so I am not able to implement the same code in Objective-C or C.
I am writing C code from the above link which I have implemented for helping you guys.
#define LOW_RATE 0.01
#define HIGH_RATE 0.5
#define MAX_ITERATION 1000
#define PRECISION_REQ 0.00000001
double computeIRR(double cf[], int numOfFlows)
{
int i = 0, j = 0;
double m = 0.0;
double old = 0.00;
double new = 0.00;
double oldguessRate = LOW_RATE;
double newguessRate = LOW_RATE;
double guessRate = LOW_RATE;
double lowGuessRate = LOW_RATE;
double highGuessRate = HIGH_RATE;
double npv = 0.0;
double denom = 0.0;
for (i=0; i<MAX_ITERATION; i++)
{
npv = 0.00;
for (j=0; j<numOfFlows; j++)
{
denom = pow((1 + guessRate),j);
npv = npv + (cf[j]/denom);
}
/* Stop checking once the required precision is achieved */
if ((npv > 0) && (npv < PRECISION_REQ))
break;
if (old == 0)
old = npv;
else
old = new;
new = npv;
if (i > 0)
{
if (old < new)
{
if (old < 0 && new < 0)
highGuessRate = newguessRate;
else
lowGuessRate = newguessRate;
}
else
{
if (old > 0 && new > 0)
lowGuessRate = newguessRate;
else
highGuessRate = newguessRate;
}
}
oldguessRate = guessRate;
guessRate = (lowGuessRate + highGuessRate) / 2;
newguessRate = guessRate;
}
return guessRate;
}
I have attached the result for some value which are different in Excel and the above C language code.
Values: Output of Excel: -33.5%
1 = -18.5, Output of C code: 0.010 or say (1.0%)
2 = -18.5,
3 = -18.5,
4 = -18.5,
5 = -18.5,
6 = 32.0
Guess rate: 0.1
Since low_rate and high_rate are both positive, you're not able to get a negative score. You have to change:
#define LOW_RATE 0.01
to, for example,
#define LOW_RATE -0.5

asp.net Column Chart is not aligning with the custom label and it does not display spaces between columns

I am trying to generate a chart to look this one
I am almost there but there are couple of issue that i can't solve.
The columns are being displayed with out a space separation between them! also the custom label that is at the very bottom is not aligning up with each column!
this is the output the i get out of my existing code
so
1) I need to spread the columns across the x axis
2) align the custom label to each column!
I appreciate any help or feedback on this problem
this is the code that generates the current image. Please keep in mind the my dataset "ds" have values like this
Emerging 28.45646456
Dent 14.1456465
Audio 27.456456
Cosmetic 43.44564456
Vet 35.15465646645
public void GenerateChart(){
//ds is generated and has values
Chart2.Series.Clear();
Chart2.Legends.Clear();
Chart2.Titles.Clear();
//if (ConfigurationManager.AppSettings["RunOnLocalhost"] == "True") {
if(HttpContext.Current.Request.Url.Host.ToLower() == "localhost"){
Chart2.ImageStorageMode = ImageStorageMode.UseImageLocation;
}
Chart2.Width = 1000;
Chart2.Height = 700;
Chart2.BorderlineDashStyle = ChartDashStyle.Solid;
Chart2.Titles.Add("Usage Impact By Industry");
Chart2.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
Chart2.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;
Chart2.ChartAreas[0].AxisX.Interval = 1;
Chart2.ChartAreas[0].AxisY.Interval = 5;
Chart2.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
Chart2.ChartAreas[0].AxisY.Title = "(%) Usage Lift";
Chart2.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 16, FontStyle.Bold);
Chart2.ChartAreas[0].AxisY.Minimum = -5;
string tmp = "";
string sName = "";
double percentage = 0;
int i = 1;
int x = 1;
double index = 0.1;
foreach (DataRow Row in ds.Tables[0].Rows) {
if (tmp != Row["Industry"].ToString()) {
sName = Row["Industry"].ToString();
Chart2.Series.Add(sName);
Chart2.Legends.Add(sName).DockedToChartArea = "ChartArea1";
i++;
}
if (Convert.ToDouble(Row["B4"]) > 0) {
percentage = (Convert.ToDouble(Row["After4"]) - Convert.ToDouble(Row["B4"])) / Convert.ToDouble(Row["B4"]) * 100;
percentage = Math.Round(percentage, 0);
}
else {
percentage = 0;
}
Chart2.Series[sName].Points.AddXY(Row["Industry"].ToString(), percentage);
Chart2.Series[sName].ChartType = SeriesChartType.Column;
Chart2.Series[sName]["PointWidth"] = ".5";
Chart2.Series[sName].IsValueShownAsLabel = true;
Chart2.Series[sName].LabelFormat = percentage + "%";
CustomLabel label = new CustomLabel();
label.FromPosition = 0 + index;
label.ToPosition = .01 + index;
label.Text = Row["Industry"].ToString();
label.RowIndex = 0;
Chart2.ChartAreas[0].AxisX.CustomLabels.Add(label);
x++;
index += 0.2;
tmp = Row["Industry"].ToString();
}
}
I think you took the different series for the X-Axis. Take a single series in your chart like,
<asp:Chart ID="Chart1" runat="server">
<Series>
<asp:Series Name="Series1" XValueType="Auto" YValueType="Int32">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
Here I have taken "Series1" in chart.
Now replace your "sName" with "Series1"
Chart2.Series[sName].Points.AddXY(Row["Industry"].ToString(), percentage);
to
Chart2.Series["Series1"].Points.AddXY(Row["Industry"].ToString(), percentage);
I think problem should be solved.

Roundoff number to greater value in Objective C

I want to roundoff my number to greater value. for example if i have 234 i want to make it 300 and 4436 to 5000. I have tried it but i can roundoff my value not in hundred and thousants but in tens. like i have a value 7771 it roundoff to 7900 or 7800 but i want it in 8000.
int temp = lroundf([[arrayPercentage objectAtIndex:i]floatValue]);
maxPer = (((temp + 10)/10))*10;
How about this:
-(void)roundUpNumber:(NSInteger) num {
NSInteger numLength = [[NSString stringWithFormat:#"%ld",num] length];
NSInteger newNum = ceil(num/pow(10,numLength-1)) * pow(10,numLength -1);
NSLog(#"%ld",newNum);
}
int num, count = 0;
int originalNumber = 7771;
num = originalNumber;
while (num) {
num = num/10;
count ++;
}
int power = pow(10,(count -1));
int firstDigit = originalNumber / power;
int finalNumber = (firstDigit + 1)* power;
NSLog(#"final result : %d",finalNumber);

Return zero or positive number?

I was initially thinking that the code below would return 0, my question, is there a function that I can use to only receive zero/positive results here?
NSUInteger pruneSize = 5 - 20; // Returns: -15
Currently I am just checking the result myself, but was wondering if I was missing something simpler.
NSUInteger pruneSize = 5 - 20;
if(pruneSize >= 0) {
// Do zero/positive Stuff ...
}
pruneSize >= 0 is always true as pruneSize is unsigned. You should get a warning here. You need to change the type to NSInteger, that is the signed integer. If you want to clip the lower value to zero for a signed int then you can do this:
NSInteger pruneSize = 5 - 20; // signed int
pruneSize = pruneSize < 0 ? 0 : pruneSize;
You can use abs(pruneSize) which will return you positive or zero number in any case.
EDIT:
NSUInteger pruneSize = 5-20;
if(pruneSize < 0)
{
pruneSize = 0;
}
NSLog(#"%d",pruneSize);
Hope this helps you.
If you want your function to return always zero if your result is in negative(less than 0) then return zero or else return result
int n=0;
if(result > 0){ //positive
n = result
else
n = 0
return n
or use the abs method