I have 1 UIPicker with 5 components, all numbers 1-9. I just want all 5 components to show up in the UITextfield. I have a feeling there is a much shorter code for this than what I have:
NSString *result = [metricMillimeterArray objectAtIndex:[metricMillimeterPicker selectedRowInComponent:0 & [metricMillimeterPicker selectedRowInComponent:1]& [metricMillimeterPicker selectedRowInComponent:2]& [metricMillimeterPicker selectedRowInComponent:3]& [metricMillimeterPicker selectedRowInComponent:4]]];
I can't tell what your objects are that are returned, but if
[metricMillimeterArray objectAtIndex:[metricMillimeterPicker selectedRowInComponent:0]
commands return strings (that are one character long) then you will need to:
result = [NSString stringWithFormat:#"%d mm",
10000 * [metricMillimeterPicker selectedRowInComponent:0] +
1000 * [metricMillimeterPicker selectedRowInComponent:1] +
100 * [metricMillimeterPicker selectedRowInComponent:2] +
10 * [metricMillimeterPicker selectedRowInComponent:3] +
[metricMillimeterPicker selectedRowInComponent:4] ];
This is assuming that your component 4 is your least significant, but the powers of 10 can be swapped if that part I got wrong.
Related
So basically I'm confused on how I'd make it so that I can convert DD:HH:MM:SS to only seconds while taking into account the amount of numbers there are. (Sorry if I make 0 sense, you should definitely know what I mean by the example below.)
print("05:00":FormatToSeconds()) -- 5 minutes and 0 seconds
-- 300
print("10:30:15":FormatToSeconds()) -- 10 hours, 30 minutes and 15 seconds
-- 37815
print("1:00:00:00":FormatToSeconds()) -- 1 day
-- 86400
print("10:00:00:30":FormatToSeconds()) -- 10 days, 30 seconds
-- 864030
So on and so forth. I think that maybe using gmatch would work but still idk. Help would be greatly appreciated.
Edit:
So I've tried doing it with gmatch, but I don't know if this is the most fastest way of doing this (which it probably isn't), so any help would still be appreciated.
(My code)
function ConvertTimeToSeconds(Time)
local Thingy = {}
local TimeInSeconds = 0
for v in string.gmatch(Time, "%d+") do
if tonumber(string.sub(v, 1, 1)) == 0 then
table.insert(Thingy, tonumber(string.sub(v, 2, 2)))
else
table.insert(Thingy, tonumber(v))
end
end
if #Thingy == 1 then
TimeInSeconds = TimeInSeconds + Thingy[1]
elseif #Thingy == 2 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60) + Thingy[2]
elseif #Thingy == 3 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60 * 60) + (Thingy[2] * 60) + Thingy[3]
elseif #Thingy == 4 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 24 * 60 * 60) + (Thingy[2] * 60 * 60) + (Thingy[3] * 60) + Thingy[4]
end
return TimeInSeconds
end
print(ConvertTimeToSeconds("1:00:00:00"))
Don't worry about execution speed before doing any actual measurements unless you're designing a time-critical program. In any extreme situation you'd probably want to offload risky parts to a C module.
Your approach is just fine. There are parts you can clean up: you can just return the results of calculations as TimeInSeconds doesn't actually act as accumulator in your case; tonumber handles '00' just fine and it can ensure decimal integers with an argument (since 5.3).
I'd go the other way and describe factors in a table:
local Factors = {1, 60, 60 * 60, 60 * 60 * 24}
local
function ConvertTimeToSeconds(Time)
local Components = {}
for v in string.gmatch(Time, "%d+") do
table.insert(Components, 1, tonumber(v, 10))
end
if #Components > #Factors then
error("unexpected time component")
end
local TimeInSeconds = 0
for i, v in ipairs(Components) do
TimeInSeconds = TimeInSeconds + v * Factors[i]
end
return TimeInSeconds
end
Of course, both implementations have problem with pattern being naïve as it would match e.g., '00 what 10 ever 10'. To fix that, you could go another route of using string.match with e.g., '(%d+):(%d+):(%d+):(%d+)' and enforcing strict format, or matching each possible variant.
Otherwise you can go all in and use LPeg to parse the duration.
Another way would be to not use strings internally, but instead convert them into a table like {secs=10, mins=1, hours=10, days=1} and then use these tables instead - getting seconds from that representation would be straight-forward.
I would like to find out the number of hours and minutes between two date time stamp.
if for example
sDateTime = 2016-01-01 01:00
eDateTime = 2016-01-03 02:30
I would like it to output it as 49:30 (49hours and 30minutes)
I am unable to figure a method to work this out.
what I have so far:
Set oMNOF=##class(MNOF.MNOF).%OpenId(Id)
Set zstartDt=oMNOF.sDateTime
Set startDt=$PIECE(zstartDt,",",1)
Set startTime=$PIECE(zstartDt,",",2)
Set zendDt=oMNOF.eDateTime
Set endDt=$PIECE(zendDt,",",1)
Set endTime=$PIECE(zendDt,",",2)
set dateDiff=((endDt - startDt)) //2 days
set timeDiff=(endTime - startTime) //outputs 5400 seconds
set d = (dateDiff * 24 * 60 * 60)
set h = ((timeDiff - d) / 60)
set m = timeDiff - (d) - (h * 60)
Thank you for the help.
Another option:
USER>set mm=$system.SQL.DATEDIFF("mi","2016-01-02 01:00","2016-01-03 02:30")
USER>write "hours=", mm \ 60
hours=25
USER>write "minutes=", mm # 60
minutes=30
Hi thanks to all for the help.
I managed to come up with the below, appreciate if someone can improve on this.
<script language="cache" method="MGetData" arguments="pStartDt:%String,pEndDt:%String,pTimeField:%String" returntype="%Library.String">
set val1="00"
//HOUR: check if length equals 1
if $LENGTH($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600))=1{
//add leading zero
set val1 ="0"_$SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)
}
else{
//get without leading zero
set val1 = $SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)
}
//MINUTES: check if length equals 1
if $LENGTH($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))=1{
//add leading zero
set val2 ="0"_($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))
}
else{
//get without leading zero
set val2 = ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))
}
//insert result data into the time field
Write "document.getElementById('"_pTimeField_"').value='"_val1_":"_val2_"';"
//Write "alert('"_val1_"^"_val2_"');"
QUIT 1
I have a float value, what i need is convert my float to date. For example i have float 50,55, i want to put on a label value like this: 50 years 6 month 6 hours 6 mins 30,04 sec.
And i wonder how to make this label change every 0.01 second for change value. Please provide me solution to convert my float value to date format, any help would be appreciated.
This is code i use for create my float:
-(float)calculationResult{
lifeTime = (kCountry +kEdu - kBirth - kSmo -kAlco+kDis+kHap+kDri+kMar)*kWei*kEnv*kSle;
return lifeTime;
}
The following code splits the given number of "fractional years" into
years, month, days and hours. It makes the simplifying assumptions that every year
has 365 days, and that one month is exactly 1/12 of a year.
float tmp = lifeTime;
int years = tmp;
tmp = (tmp - years) * 12.;
int months = tmp;
tmp = (tmp - months) * 365./12.;
int days = tmp;
tmp = (tmp - days) * 24.;
int hours = tmp;
NSString *str = [NSString stringWithFormat:#"%d years %d month %d days %d hours ",
years, months, days, hours];
To update a label regularly, you can use the NSTimer class (but updating every 1/100 second is much too often).
I am making a grading App for my students, but my comparison operators are not functioning they way I expect them to. My code is as follows;
float FINAL = ((_gradeyouwant - (_quarter1 * 0.2f) - (_quarter2 * 0.2f) - (_quarter3 * 0.2f) - (_quarter4 * 0.2f) - (_quarterM * 0.1f)) / 0.1f);
NSLog(#"q1 = %.2f", _quarter1);
NSLog(#"q2 = %.2f", _quarter2);
NSLog(#"q3 = %.2f", _quarter3);
NSLog(#"q4 = %.2f", _quarter4);
NSLog(#"qM = %.2f", _quarterM);
NSLog(#"qF = %.2f", FINAL);
NSLog(#"grade = %.2f", _gradeyouwant);
if ((FINAL > 4.3f))
{
[_result setText:[NSString stringWithFormat:#"It is not possible to get your desired grade."]];
}else if ((FINAL > 4.0f))
{
[_result setText:[NSString stringWithFormat:#"You would need to get an A+"]];
}else if ((FINAL > 3.7f))
{
[_result setText:[NSString stringWithFormat:#"You would need to get an A"]];
}else if ((FINAL > 3.3f))
ETC. ETC.
When you look at the output with NSLog, it tells me the correct value of everything. However, if I make it so the FINAL is 4.0, it does not print the correct string. I was figuring that when it got to the FINAL > 4.0, it would not run that line. But it does. What am I doing wrong? Thanks so much in advance!
This is pretty much how floats work. Google it, e.g. http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
The system may not be able to precisely store 4.0. It's more a limitation of your CPU and choice of data types. Using a range may very well work.
I'd use an int and emulate the decimal digits, e.g. GPA * 100.
How to disable symbols after dot in double values?
13 + 3.456 = 16.456 GOOD
13 + 1 = 14.00 BAD! I need 14 how can I do It?
I am using NSMutableString.
if ([yourString doubleValue] % 1 == 0) {
[yourString setString:[NSMutableString stringWithFormat:#"%d", [yourString intValue]]];
}