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".
Related
Maybe i'm going about this wrong, and if i am please tell me because i just dont know any better.
But i am trying to pass in a CCString into the following format, and not having any luck. Could someone please tell me what the parameter for strings would be in C++ when passing them into another string?
Code:
CCString tilt = "";
if (recalculatedFrames >= 4 && (numberOfTimesRun > 0 && numberOfTimesRun < recalculatedFrames - 1)) {
tilt = "TR_";
}
initalTurnAnimationFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::stringWithFormat("%s%d.png", tilt, i)));
Also is it fine to make a blank string like i am doing with tilt?
I think tilt should be class of string
And the CCString part should be
CCString::createWithFormat("%s%d.png", tilt.c_str(), i);
try this
CCString::stringWithFormat("%s%d.png", tilt.getCString(), i)
This question already has answers here:
Constants in Objective-C
(14 answers)
Closed 9 years ago.
I have the following function:
+ (int)isCorrectOnServer:(int)num {
// some code performs here...
if (this)
{
result = 2;
}
else if (this2)
{
result = 1;
}
else
{
result = 0;
}
return result; }
I want it to return like this instead:
+ (int)isCorrectOnServer:(int)num {
// some code performs here...
if (this)
{
result = kOnServer;
}
else if (this2)
{
result = kWrongType;
}
else
{
result = kNotOnServer;
}
return result; }
So anywhere in my code I could just call:
if ([Helper isCorrectOnServer:276872] == kOnServer)
to make my code cleaner and not show 0, 1, or 2 as the result. What is the best way to go about doing this?
When in doubt, see what Apple do. Look in any header file in UIKit and you will see enumeration being used for any kind of value with a finite amount of options.
Just put this in the header file:
typedef enum {
MYCustomTypeOne,
MYCustomTypeTwo,
MyCustomTypeEtcetera
} MYCustomType;
Using proper enumerations over defines allow you to do define a method like this:
+(BOOL)isCustomTypeCorrectOnServer:(MYCustomType)type;
Then the argument can be auto completed for you by Xcode. And the compiler can make much better assumptions if you use the value in a switch case for example.
add the contants to the prefix header file of your project, usually named prefix.pch
Or, an even more clean solution, would be to create a contants file, like contants.h and include this file in your prefix header file.
Prefix.pch
#include Contants.h
Contants.h
#define kMyCustomName 1
#define kMyOtherCustomName 2
#define kMyCustomName 1
#define kMyOtherCustomName 2
Within the scope that they will be used, as Felipe Sabino said to make them truly global they should be in the prefix header.
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.
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
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
}
}