// stampDuty percentages
float stamp1 = (invalue * 1 / PCENT);
float stamp2 = (invalue * 2 / PCENT); // #defined PCENT as 100
// stamp duty
NSNumber *stampN1 = [NSNumber numberWithFloat:stamp1];
NSNumber *stampN2 = [NSNumber numberWithFloat:stamp2];
// then goes to
// Stamp Duty invalue is entered in the textfield by user
if (invalue <= 125000.0f) {
NSLog(#"Equal to or under 175,000 1% Stamp Duty");
//stampN0
[lblIntrest setText:[NSString stringWithFormat:#"Stamp Duty: %#", [currencyStyle stringFromNumber:stampN1]]];
// need a common variable variable is picked up here
// the value that is calculated here is used with other totals to create a grand final total
}
if (invalue >= 125001.0f && invalue <= 250000.0f) {
NSLog(#"Greater than 125,000 and less than 250,000 StampDuty 1%%");
[lblIntrest setText:[NSString stringWithFormat:#"Stamp Duty: %#",[currencyStyle stringFromNumber:stampN2]]];
// need a common variable is picked up here
}
// returns with the appropriate value
//the value that is calculated here is used with other totals to create a grand final total.
// eg float Beven = (invalue + SaleP + stamp1 etc etc
If I use stamp1 in the above calculation it works fine. What I'm looking for is a common variable to enter into this calculation variable "string". There are other if statements.
Hope you can help
I suggest the variable name actualStampValue.
Also note, your code contains two errors:
Your first if checks that the number is <=125000, but the string it prints says 175000.
Your second if checks that the number is >=125001, which leaves out all the values in between 125000 and 125001. This check should be >125000.
Related
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 surcharge text box that I want to perform calculations based on a subtotal field. There is a minimum 4.50 which is calculated when the subtotal is <112.5. When the subtotal is >=112.5 the calculation is subtotal *0.04. My problem is that I don't know how to program the field to show as empty when the subtotal is 0.
Here is my code.
{
var nSubtotal = this.getField("Subtotal").value;
if(nSubtotal = "0")event.value = "";
if( nSubtotal >= 112.5) event.value = nSubtotal * 0.04;
if( nSubtotal < 112.5) event.value = 4.50;
}
This can be simplified (and be made to work) with something like this:
if (this.getField("Subtotal").value > 0) {
event.value = util.printf("%.2f", Math.min(560, Math.max(4.50, this.getField("Subtotal").value * 0.04))) ;
} else {
event.value = "" ;
}
Note that in this case, the formatting of the result field is done in the calculation, and therefore, the result field does NOT need any format set; this also prevents the result field to show a value based on incomplete calculations.
guys, I'm making simple graph drawer and want to find beautiful values for horizontal lines.
For example, if I have value equals to 72089.601562, beautiful is 70000, or 75000. So, I think that beautifulNumber%5 = 0.
Have you any ideas?
How about this?
#import <math.h>
#import <stdio.h>
#define ROUNDING 5000
int beautify(float input)
{
// Cast to int, losing the decimal value.
int value = (int)input;
value = (value / ROUNDING) * ROUNDING;
if ((int)input % ROUNDING > ROUNDING / 2 )
{
value += ROUNDING;
}
return value;
}
int main()
{
printf("%d\n", beautify(70000.601562)); // 70000
printf("%d\n", beautify(72089.601562)); // 70000
printf("%d\n", beautify(76089.601562)); // 75000
printf("%d\n", beautify(79089.601562)); // 80000
printf("%d\n", beautify(70000.601562)); // 70000
return 0;
}
It depends whether you want a floor value, a ceiling value or just to round to the nearest 5000.
For a floor value:
int beautiful = (int)(floor(ugly / 5000.0) * 5000.0);
For a ceiling value:
int beautiful = (int)(ceil(ugly / 5000.0) * 5000.0);
For rounding:
int beautiful = (int)(round(ugly / 5000.0) * 5000.0);
For making graph lines, I'd probably find the minimum and maximum values you have to graph, start with a floor value for the minimum value and then add your desired interval until you have surpassed your maximum value.
For instance:
float minValue = 2.34;
float maxValue = 7.72;
int interval = 1;
NSMutableArray *horizLines = [NSMutableArray array];
int line = (int)(floor(minValue / interval) * interval);
[horizLines addObject:[NSNumber numberWithInt:line]];
do {
line = (int)(ceil(minValue / interval) * interval);
[horizLines addObject:[NSNumber numberWithInt:line]];
if (minValue >= maxValue) break;
minValue = minValue + interval;
}
Use as needed!
Well, it seems like you'd want it to scale based on the size of the number. If the range only goes to 10, then obviously rounding to the nearest 5,000 doesn't make sense. There's probably a really elegant way to code it using bit shifting but just something like this will do the trick:
float value = 72089.601562
int beautiful = 0;
// EDIT to support returning a float for small numbers:
if (value < 0.2) beautiful = int(value*100)/100.;
else if (value < 2.) beautiful = int(value*10)/10.;
// Anything bigger is easy:
else if (value < 20) beautiful = (int)value;
else if (value < 200) beautiful = (int)value/10;
else if (value < 2000) beautiful = (int)value/100;
else if (value < 20000) beautiful = (int)value/1000;
// etc
Sounds like what you want to do is round to 1 or perhaps 2 significant digits. Rounding to n significant digits is pretty easy:
double roundToNDigits(double x, int n) {
double basis = pow(10.0, floor(log10(x)) - (n-1));
return basis * round(x / basis);
}
This will give you roundToNDigits(74518.7, 1) == 70000.0 and roundToNDigits(7628.54, 1) == 8000.00
If you want to round to 1 or 2 digits (only 2 where the second digit is 5), you want something like:
double roundSpecial(double x) {
double basis = pow(10.0, floor(log10(x))) / 2.0;
return basis * round(x / basis);
}
Please can anyone help me out here, I wish to display the following:
for (int i = 0; i < [highScores count]; i++)
{
[scoresString appendFormat:#"%i. %i\n", i + 1, [[highScores objectAtIndex:i] intValue]];
}
As a time value in this format 00:00:00 (in minutes seconds and hundredths of a second). At the moment I am getting values such as 7008(note that i have my maximum time in seconds is defined as *# define MAX_TIME 7200*).
Please how do about doing this conversion.
As X Slash states, if your integer is in seconds then you can't get hundredths of a second, but to convert it to minutes and seconds, do this:
int total = [[highScores objectAtIndex:i] intValue];
int minutes = total / 60;
int seconds = total % 60;
[scoresString appendFormat:#"%i. %02i:%02i\n", i + 1, minutes, seconds];
Note the 02 between the % and i - that means space out the number to two characters and insert leading zeros.
Incidentally, if your seconds are coming out as 7008 then with the logic above you'll get a time of 116:48 - i.e. 116 minutes, 48 seconds. Are you sure your time is in seconds? If it is you may wish to add an hours column.
In my application I have a music player, which plays music with a length of 0:30 seconds.
However in a UILabel I am currently displaying the progress, and as it is a float, the label is displaying i.e 14.765.
I would appreciate it if you could tell me how I could get the label to display
0:14 rather than 14.765.
Also, I would appreciate it if you could tell me how I could display 0:04 if the progress was 4seconds in.
This works properly:
float time = 14.765;
int mins = time/60;
int secs = time-(mins*60);
NSString * display = [NSString stringWithFormat:#"%d:%02d",mins,secs];
Results:
14.765 => 0:14
30.000 => 0:30
59.765 => 0:59
105.999 => 1:45
EDIT
In addition the 'one liner':
float time = 14.765;
NSString * display = [NSString stringWithFormat:#"%d:%02d",(int)time/60,(int)time%60];
You first need to convert your float to an integer, rounding as you wish. You can then use the integer division, /, and remainder, % operations to extract minutes and seconds and produce a string:
float elapsedTime = 14.765;
int wholeSeconds = round(elapsedTime); // or ceil (round up) or floor (round down/truncate)
NSString *time = [NSString stringWithFormat:#"%02d:%02d", wholeSeconds/60, wholeSeconds%60];
The %02d is the format specification for a 2-digits, zero padded, integer - look up printf in the docs for full details.
//%60 remove the minutes and int removes the floatingpoints
int seconds = (int)(14.765)%60;
// calc minutes
int minutes = (int)(14.765/60);
// validate if seconds have 2 digits
NSString time = [NSString stringWithFormat:#"%i:%02i",minutes,seconds];
that should work. Can't test it i'm on Win currently