kCFNumberFormatterScientificStyle and kCFNumberFormatterDecimalStyle hybrid? - iphone / objective-c - iphone

in objective-c or iphone development has anyone ever done dynamic number formatting - something along the lines of "use kCFNumberFormatterDecimalStyle until the number gets too big, then use kCFNumberFormatterScientificStyle instead?"
i want to display a number with some sort of hybrid between the two, but i'm having a little trouble with implementing it. thanks in advance.

An other way would be selecting the formatter inline using the elvis operator:
// assuming you have formatter declared previously
// and x is the float NSNumber you want to format
formatter = ([x floatValue] < 1000.0) ?
kCGNumberFormatterDecimalStyle :
kCGNumberFormatterScientificStyle;
// Format with formatter
You would probaly want to put all of this in a #define or a method though.

I'd say create a method for that somewhere:
NSString *NSStringFromNumberInHybridStyle(NSNumber *aNumber)
{
if ([aNumber intValue > 100]) {
// format with kCGNumberFormatterDecimalStyle
} else {
// format with kCGNumberFormatterScientificStyle
}
}

Related

How to know if a float got a decimals

I'm building a simple calculator for a classroom, this is the code to show the result:
- (void)doTheMathForPlus:(float)val {
float conto = self.contBox + val;
self.myDisplay.text = [NSString stringWithFormat:#"%.02f", conto];
}
I need to know if "conto" have decimals (to change the format of the string in a if statement)
how to do it?
thanks
if (conto == (int)conto) {
}
Hope this helps.
you'd better wrap that with #define and put this line in your .pch file
#define HAS_DECIMALS(x) (x!=(int)x)
than use anywhere in the code:
if (HAS_DECIMALS(conto)) {
//number has decimals
} else {
//number does not have decimals
}
Yes, dredful is right with his answer.
And, You also can use different functions like floor, ceil, round. In You case better "floor(conto)".
But, you can't do like Kyr Dunenkoff suggested. becouse, all operands for"%" should be integer, but not float as "conto".

notation problem

When i typed [lat1 = newLocation.coordinate.latitude]; its telling expected : before ] token like that. whats my fault? as i am new to this domain., please ayone guide me.
Thanks In Prior....
If you are trying to compare lat1 and newLocation.coordinate.latitude, the correct statement would be:
if (lat1 == newLocation.coordinate.latitude) {
// do something here
}
If you are trying to assign the value of newLocation.coordinate.latitude into lat1, the correct statement is:
lat1 = newLocation.coordinate.latitude;
If you are trying to do the first thing and the compared variables are floating point numbers, then you probably want to check if they are close enough instead of equality:
if (fabs(lat1 - newLocation.coordinate.latitude) < someLittleDistance) {
// close enough
}
…where of course you will have to define someLittleDistance.
Try the following..
Remove the [] brackets from your line it must be
lat1 = newLocation.coordinate.latitude;
When xCode behaves like this, it probably wants to say that something is the method or it thinks of something like a method. Dot-notation in Objective-C as usual is some kind of equivalent of the setter. For example
ObjectA.property1 = value;
is equivalent of
[ObjectA setProperty1:value];
And in the last case, xCode expects to see : after the setter call and a value after the column.

Create NSString with 'N' Unicode Characters

I'm trying to create a string containing (unicode) 'stars' based on an integer rating. I currently have:
NSMutableString *stars = [NSMutabelString stars];
for (int i = 0; i < rating; i++)
{
[stars appendString:#"\u2605"];
}
However, I find this a bit ugly. Does a way exist to construct such a string without using this looping method? Something using the string formats?
Sure - to do this on a single line you can use the stringByPaddingToLength method:
[#"" stringByPaddingToLength: rating withString: #"\u2605" startingAtIndex:0];
...should hopefully do the trick for you - and no need to create any subclasses or categories, etc!
You can make a category for NSString with an extra method - say, +(NSString)stringForRating:(NSInteger)rating, and move the loop in there. Then whenever you need a star string, just call that.

Comparing to NSDates not working, incompatible pointer types

Hey i am trying to compare the date on which the user opens the app to the date it is currently. (basically, how long they've had the app in days)
Here is the code:
- (NSInteger) daysAfterDate: (NSDate *) aDate
{
NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; //#1
return (NSInteger) (ti / D_DAY); //#2
} //#3
-(void)load {
NSDate *birthdate = [prefs objectForKey:#"Birthdate"];
rock_Age = daysAfterDate(birthdate);
}
errors:
1.) it tells me incompatible types in initialization
2.) D_DAY Undecared
warning:
3.) control reaches end of non void function
If i did this completely wrong, (because for the life of me i cannot understand the NSDate class :/) I would gladly take an alternative to doing this :)
all help is appreciated,
Thank you
-Jackson Smith
The load method is correct, the following should work for -[daysAfterDate:].
#define D_DAY 86400
-(NSInteger)daysAfterDate:(NSDate *)aDate {
NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:aDate];
return (NSInteger) (ti / D_DAY);
}
1) is because self is probably no NSDate. Use [NSDate date] to get the current time/date.
2) is because you have to define D_DAY
3) only happens because of 2).
I hope this helps.
The following article might also be informative: (Ars Technica)
Does the local method timeIntervalSinceData return an "NSTimeInterval" (typdef double)? I'm guessing it doesn't, hence the error - but the code isn't here to see.
We need to see a bit more code to help you - but the D_DAY undeclared should be easy to resolve. Assuming it's not a #define somewhere in your headers, you need to specify what it is in that function or higher up in the file. I'm guessing you're just missing a #define somewhere that puts in a specific value - at least by syntax.
The warning is because of this error - the parser doesn't know how to complete things nicely until you've fixed that.
for 1) i think it talks about "self" ;it is a date?

hot do i make a comparison of integers and values of text box in Objective C

i'm new to iphone programming and i encountered/noticed some problems while i was coding. which was after i typed a statement like
if (label.text > label2.text) {
do something...}
however, after typing my application can be compiled and run however when i try to validate it by comparing the values, my specified actions can run and i can change my image view image, however the conditions is not true but the specified actions can be run. Do enlighten me thanks! i will post my codes at the bottom, do comment if you spot any better practices? thanks once again.
Oh and how can i specify to check in my label that the default value is not "Label" or empty because i would like the values to be populated with number before commencing.
-(IBAction) beginMatch {
if (resultP1.text, resultP2.text = #"Label") {
errorMsg.text = #"Please Press Roll (:";
}
else
if (resultP1.text > resultP2.text) {
MG = [MainGameController alloc];
MG.player1 = playerName.text;
}
else {
MG.player1 = playerNameP2.text;
}
[self.view addSubview:MG.view];
}
this is one example that it does not work i have another one which is below.
-(IBAction) btn:(id) sender {
ptc = [playerTurnController alloc];
if (ptc.player1name = MGplayerName.text) {
if (lblDiceResultP1.text > lblDiceResultP2.text) {
img.image = [UIImage imageNamed:#"yellow.png"];
}
else if (ptc.player2name = MGplayerName.text) {
img.image = [UIImage imageNamed:#"Blue.png"];
}
}
}
Thank you.
Your code contains quite a few errors. You're trying to compare NSString values with ">", you're using the comma operator and = operator incorrectly, and you're allocating new objects in (what look to be) the wrong places.
You really should work your way through the introductory documentation on Apple's developer website first:
Learning Objective-C: A Primer
and
Your First iPhone Application
In here you're comparing string (alphabetically) addresses:
lblDiceResultP1.text > lblDiceResultP2.text
You probably want to extract NSNumbers of out the strings and compare the numeric values.
This here is an assignment and not a comparison:
ptc.player2name = MGplayerName.text
You probably meant to use == which is also wrong.
NSStrings are compared with the isEqualToString e.g.
NSString * s1 = #"String One";
NSString * s2 = #"String Two";
if([s1 isEqualToString:s2])
// do something when strings are equal