string/number from TextField to be used for calculation? - cocoa

sorry for my question and for the long explanation following here, probably there's a simple solution for someone who is experienced in Cocoa and Objective-C, but I'm just starting few weeks ago and I can't figure out how to get this thing working, Grrrrrrr!!
OK, let's put it like this, in my window I have the following:
2 TextField(NSTextField) called:
blockOffText
blockOnText
1 Label(NSTextField) called:
flightTimeText
1 button(NSButton) called:
updateButton
What I want to do is all about time calculation I guess, get the "start-time" in one TextField and the "end-time" in the other.
One is supposed to be the "takeoff-time" and the other the "landing-time" or for example the "duty-startTime" and "duty-endTime"...it's the same!
Then I want to calculate the "flight-time" or "duty-time" and show it in the Label.
At the end I will also need to store the value or the time in a database as an integer, the value should be represented by all minutes corresponding to each time, but the database part is not a problem at the moment.
Maybe I can write the number in the TextField without the format but just the number and get the time show-up formatted in some way?
I would like to write for example "1245" and "1525" without having the needs to put the ":" between the hours and minutes, then can I get the value formatted "12:45" "15:25" in the TextField in some way? Maybe after pressing the button?
Ok, that is a second problem anyway, my real problem is I need to get the value I wrote in the TextField ("1245" and "1525") to be assigned to some variables in the program that I called "BlockOff" and "BlockOn".
I need to transform them in an integer that represent the minutes corresponding to their value..example:
The 1245 will become 765 minutes...(12 times 60 + 45)
and 1525 will become 925 minutes...(15 times 60 + 25)
HOW CAN I DO THIS?
In this way I can use the minutes to calculate the difference to get the flight time or even add flight time to other flight time.
At the moment my program works a little bit differently... like this:
If I directly assign the value to the two variables:
int blockOff = 765;
int blockOn = 925;
then I can calculate and show in the two TextField the takeOff and landing time formatted like I want: "12:45" "15:25"...I use other 2 variables to do so:
int oreBlkOff, minBlkOff = 0;
minBlkOff = blockOff % 60;
oreBlkOff = (blockOff - minBlkOff) / 60;
Then I can show the value in the TextField:
[blockOffText setStringValue:[NSString stringWithFormat: #"%d:%d", oreBlkOff, minBlkOff]];
Same with blockOnText and flightTimeText, so there is no problem there, but this is not really what I need right?
HOW CAN I GET THE VALUE OF THE TEXTFIELD AND STORE THE VALUE IN VARIABLES THAT CAN BE USED TO DO CALCULATION?
HOW DO I GET THE FIRST TO DIGITS AND LAST TO DIGITS FROM THE VARIABLES SO I CAN USE THEM AS HOURS AND MINUTES FOR MY CALCULATION?
I WOULD LIKE TO BE ABLE TO TYPE THE VALUE IN THE TEXTFIELD, PRESS "ENTER" OR "RETURN" AND GET THE VALUE ASSIGNED IN THE VARIABLE.
IS THIS THE CORRECT WAY OR I'M JUST GOING THE WRONG WAY IN THIS.
IN ANOTHER PROGRAM I DID IN VISUAL BASIC THAT WAS THE WAY I USED.
THANK YOU VERY MUCH IN ADVANCE FOR YOU HELP!
Gianluca

To get the int value from text field you'd use integerValue or floatValue for float. But you should definitely check out the date/time controls in cocoa.

Ok, if I didn't miss the point of your long question...
You have a text field with something like #"2345:8743" and you want to get the two numbers in int variables (a=2345, b=8743).
First you need to get these two text representations of your numbers in two NSStrings. You can do it like this:
NSString * yourString = #"2345:8743";
NSString *stringA = [yourString substringToIndex:4]; //stringA = #"2345"
NSString *stringB = [yourString substringFromIndex:5]; //stringB = #"8743";
Now to get the in values, just do:
int a = [stringA intValue];
int b = [stringB intValue];
Let me know if that is helpful to you.

Related

JqGrid - updating the values in all data cells

My JqGrid looks like this:
Staff
Room 1
Room 2
Jim
240
120
Dave
480
240
The staff and rooms are obtained from tables and are unknown in number at runtime. The figures (data) represent the total time spent by each staff in each room and are in minutes. I have all the above working.
All I want to do now is to iterate over all the data entries and change minutes (Eg: 240) to hours and minutes (EG 4h:0m). I'm ok with doing the math for the conversion, it's the looping over the cells and the reading and updating (just the displayed value) that has defeated me.
This is my code so far for the looping:
var $grid = jQuery('#statstab1grid')
var rows = $grid[0].rows
var crows = rows.length
var irow, row, cellsofrow
for (irow = 1; irow < crows; irow++)
{
row = rows[irow];
cellsofrow = row.cells;
alert('$(cellsofrow[0]).text() is ' + $(cellsofrow[0]).text())
alert('$(cellsofrow[1]).text() is ' + $(cellsofrow[1]).text())
}
The first alert outputs Jim then Dave, the second alert outputs nothing,
just the prompt. Even if I managed to access the data values, how would I write back to the grid the modified values?
It is good to post which version of jqGrid is used. This one is the important part.
The code you posted and your comments that nothing is alerted for cell index 1, can tell me that maybe you have a hidden field in your colModel, which value is empty. In this case it would be good to post your entire grid setup.
To the problem - you have a lot of options to do this conversion.
You can use custom formatter - more about this you can find here. This method is preferred.
You can use getRowData (without parameter) to get all the data in the grid and use setRowData to update the values. Be a careful with this method if you have a lot of data in the grid - it will be slowly in this case. See docs for grid methods
If your data is local (array) you can recalculate it, before to put it into the grid

Dividing by half in ruby to create an effective calculator

For the past while I've been working on a calculator, but have run into problems when needing to divide by a half. I'll add the offending bit of code along with a loop to keep it open below.
on = true
while on == true do
half = 1.0 / 2.0
puts ("FUNCTION IN TESTING MODE, DO NOT EXPECT IT TO FUNCTION PROPERLY")
puts ("Area of a triangle")
print("What is the legnth of the base? ").to_i
base = gets.chomp("base")
print("\nWhat is the height? ")
height = gets.chomp("height").to_i
PreAreaT = base * height
AreaT = PreAreaT * half
puts("The area of the triangle is #{AreaT}")
end
So essentially, how on Earth do I get the program to display an answer, rather than outputting nothing for the answer?
EDIT:As it would turn out the code above is improperly done. I've spent nearly two weeks asking myself why it wouldn't work only to find I had .to_i after a print statement rather than the input.
Your to_i call is switched around here.
print("What is the legnth of the base? ").to_i
base = gets.chomp("base")
Should be the other way 'round.
print("What is the length of the base? ")
base = gets.chomp("base").to_i
Further, chomp will attempt to remove any occurrences of base or height from the string. Be sure that you're intention is to remove those occurrences; if you want to remove whitespace, you'll have to take a different approach.

unity3d - Change Image.fillAmount

I have an image acting as a health bar, and i want to give it a cistume value more than 1:
public Image healthBar;
// Use this for initialization
void Start () {
float health = 4;
healthBar.fillAmount = health;
Debug.Log (gameObject + ""+ healthBar.fillAmount);
}
Problem is no matter what value I give health, the fillAmount always goes back to 1. Is there any way to make it higher ?
fillAmount is a value of 0-1, with at 1 the full image being shown. There is no behavior yet for numbers over 1.
You could change the width of the image based on the maximum health and calculate the percentage that needs to be filled.
healthBar.rectTransform.rect.width = maxHealth * healthBarWidth;
healthBar.fillAmount = health / maxHealth;
If you don't want one bar, but multiple images like hearts that still are filled partially, you might be able to do it with a tiled image which automatically truncates at the edges.
I think you made a spelling error when creating the heath float. You call health when you declared "healt"
healthBar.fillAmount += health;
Try to do the increment in the fillAmount value, as it will assign the value you want it, and then change the code little bit in order to reach the exact number.

adding numbers in uitextfield to update uilabel

I have code with several uitextfields that will be used to input numbers, and I want to add these numbers together to update a uilabel.
I can do all the updating and the labels and fields, but can't get the addition to work.
Just now I have:
label.text = (textfield1.text + textfield2.text);
I assume I need to convert these textfield inputs to an int, but not sure how to do that...
there is a couple of extra steps you have to do:
convert the string value of your text filed into numerical value
do the math there
and convert it back.
For example (i use float in my case, you can change that to whatever type you want):
float textField1Value = [textfield1.text floarValue];
float textField2Value = [textfield2.text floarValue];
label.text = [NSString stringWithFormat:#"%f", textField1Value + textField2Value];
Hope that helps.

How do I get the line of text under the cursor in a TextView in gtk#?

I have a GTK# TextView and I want to read the line of text under the cursor. I don't see a single method that will do that, so I think I need to combine several method calls, like Buffer.GetText, Buffer.GetIterAtOffset, Buffer.CursorPosition, but it's not obvious to me what the right combination is.
TextIter are a bit odd to use. Buffer.CursorPosition gives you the current position.
It's easy to find the end of the line:
var end = Buffer.CursorPosition;
end.ForwardToLineEnd();
To get the first character, there's not symetrical method, so you might try:
var start = Buffer.CursorPosition;
start.BackwardChars(start.LineOffset); // LineOffset gives you the iter offset on the current line.

Resources