remove decimal when value is equal 10 else not - ios

How to remove decimal if value is equal to 10 else decimal value should not removed.
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
if (_ratingValue >= 100) {
_ratingValue = 10;
_formatValue = #"%0.0f";
}
else{
_formatValue = #"%.01f";
}
}
_ratingValue == 0.0f ? [cellProductInfo.view_ProductRating setInnerText:#"review"] :
[cellProductInfo.view_ProductRating setInnerText:[NSString stringWithFormat:#" %.01f / 10",_ratingValue]];
If ratingValue is equal 10, I do not want to show the decimal value else if below 10 want to show decimal value.

For 1 decimal place your format needs to be %.1f. For no decimals you want %.0f.
If you want values less than 10 to show 1 decimal and values greater or equal to 10 to show none, you want:
if (_ratingValue >= 10.0) {
_formatValue = #"%.0f";
} else {
_formatValue = #"%.1f";
}

_ratingValue == 0.0f ? [cellProductInfo.view_ProductRating setInnerText:#"review"] :
[cellProductInfo.view_ProductRating setInnerText:[NSString stringWithFormat:_formatValue,_ratingValue]];
Its works for me!

Related

How can I show first and end values in Chart objective c?

I'm using ios-chart (ios Charts). I have some values to show xAxis, but when I load a type of graph, I only want show 2 or 3 values. The first, the mid and the last value.
But i only can show 1 value at first if the values are more than 30 aprox.
I think I need override the stringForValue function, how can override this to show almost one value at finish and at begining?
- (NSString *)stringForValue:(double)value
axis:(ChartAxisBase *)axis
I set values array in format like:
xValuess: (
"1",
"",
"",
"",
"65",
"",
"",
"",
"140"
)
But the values received in the function override islike format: 0, 50, 100. I don't know how to get the concret values of my xValues array.
1- First you need to add <IChartValueFormatter>
2- Then you should set your LineChartDataSet's valueFormatter to self
set1.valueFormatter = self;
3- And finally you can reach your data from;
-(NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler{
if (entry.x == 0 || entry.x == myArray.count-1) {
NSInteger index = [NSNumber numberWithDouble:value].integerValue;
return [NSString stringWithFormat:#"%ld",(long)index];
} else {
return #"";
}
}
I hope it helps.

Change an integer value after a time interval ios

I am making a game with powerups. I have 3 powerups. When a powerup is active, I use an integer to identify which one it is, 1, 2, or 3, with 0 representing no active powerups. Once I activate a powerup, I want it to expire after a time interval, say 10 seconds. How do I reset the identifier integer back to 0?
For example, one powerup speeds up the ship. Here is my update method.
-(void) update:(ccTime)dt
{
if (powerupIdentifier == 0)
{
shipSpeed = 100;
}
if (powerupIdentifier == 1)
{
shipSpeed = 200;
}
CCArray* touches = [KKInput sharedInput].touches;
if ([touches count] == 1)
{
//MAKES SHIP MOVE TO TAP LOCATION
KKInput * input = [KKInput sharedInput];
CGPoint tap = [input locationOfAnyTouchInPhase:KKTouchPhaseBegan];
ship.position = ccp( ship.position.x, ship.position.y);
if (tap.x != 0 && tap.y != 0)
{
[ship stopAllActions]; // Nullifies previous actions
int addedx = tap.x - ship.position.x;
int addedy = tap.y - ship.position.y;
int squaredx = pow(addedx, 2);
int squaredy = pow(addedy, 2);
int addedSquares = squaredx + squaredy;
int distance = pow(addedSquares, 0.5);
[ship runAction: [CCMoveTo actionWithDuration:distance/shipSpeed position:tap]];//makes ship move at a constant speed
}
}
}
First, use an enum rather than an int.
typedef NS_ENUM(unsigned short, PowerUp) {
PowerUp_Level0,
PowerUp_Level1,
PowerUp_Level2,
PowerUp_Level3
};
Enum is far more readable, and is a lot more self-documenting then random integers.
Now, say we have a property:
#property (nonatomic, assign) PowerUp powerUp;
We can write a method to reset the power up:
- (void)resetPowerUp {
self.powerUp = PowerUp_Level0;
}
Now, when we've set it to some non-zero value and need to reset it (after 10 seconds), it's as simple as this:
self.powerUp = PowerUp_Level2;
[self performSelector:#selector(resetPowerUp) withObject:nil afterDelay:10.0f];

if x > 0 but less than y

Sorry for kind of stupid question.
I use an UITextField, where I can enter some numbers.
No i use this code to detect if the entered number is 0, greater than 0, and less than 15.
if (myNr <= 15){
NSLog (#"the number is OK");
}
else if (myNr > 15)
{
NSLog(#"this number doesn't existing");
}
else if (myNr == 0)
{
NSLog(#"number should be between 1 and 15");
}
I got some errors when the number is 0.
i Need to be able to insert numbers only between 1 and 15, if the number is 0 or greater then 15, NSLog should say.
thanks
you should put if(myNr == 0) at first
if (myNr == 0)
{
NSLog(#"number should be between 1 and 15");
} else if (myNr <= 15)
{
NSLog (#"the number is OK");
}
else if (myNr > 15)
{
NSLog(#"this number doesn't existing");
}
What error do you get?
Offhand, remember: Capitalization matters. NSLog is different than nslog.
Your code reduces to:
if (myNr <= 15)
{
NSLog(#"the number is OK");
}
else
{
NSLog(#"this number doesn't existing");
}
The case for 0 is never reached.
Remember the tests are considered sequentially.

How to compare decimal values

I have a problem with comparison two decimal values.
I have a text field that contains number like 0.123456 and NSNumber that contains 0.000001.
Maximum fraction digits of both is 6. Minimum - 0
I've tried to do it like that:
NSNumberFormatter *decimalFormatter = [[NSNumberFormatter alloc] init];
[decimalFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
[decimalFormatter setMaximumFractionDigits:6];
double sum = [[decimalFormatter numberFromString:self.summTextField.text] doubleValue];
if (self.minSum != nil) {
if (sum < [self.minSum doubleValue]) {
return NO;
}
}
But i have a problem, that sometimes 0.123456 = 0,123455999... or 0,123456000000...01
For example #0.000001 doubleValue < #0.000001 doubleValue - TRUE.
How can I compare to NSNumber with a fractional part, to be sure that it will be correct?
Thanks in advance.
Create extension to decimal for rounding
extension Decimal {
func rounded(toDecimalPlace digit: Int = 2) -> Decimal {
var initialDecimal = self
var roundedDecimal = Decimal()
NSDecimalRound(&roundedDecimal, &initialDecimal, digit, .plain)
return roundedDecimal
}
}
let value1 = Decimal(2.34999999).rounded(toDecimalPlace: 4)
let value2 = Decimal(2.34999989).rounded(toDecimalPlace: 4)
print(value1.isEqual(to: value2))
this results in TRUE
You can round your value, if you worried about fractional part...
Something like this:
-(double)RoundNormal:(double) value :(int) digit
{
value = round(value * pow(10, digit));
return value / pow(10, digit);
}
And then compare it.
You can simply put the test otherwise if you do not want to bother much
if(abs(x-y) < 0.0001)
This should solve it:
NSNumberFormatter *decimalFormatter = [[NSNumberFormatter alloc] init];
[decimalFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
[decimalFormatter setMaximumFractionDigits:6];
[decimalFormatter setMinimumFractionDigits:6];
[formatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[formatter setRoundingIncrement:[NSNumber numberWithDouble:0.000001]]
Use the NSDecimalNumber class - see the guide Number and Values Programming Topics
This is how NSDecimal numbers are compared in iOS:
if ( [x compare:y] == NSOrderedSame ){
// If x is equal to y then your code here..
}
if([x compare:y] == NSOrderedDescending){
// If x is descendant to y then your code here..
}
if([x compare:y] == NSOrderedAscending){
// If x is ascendant to y then your code here..
}

making sure that every new position is different from previous two positions

Below is a method I wrote that takes a random number and makes sure that a sprite does repeat consecutively at the same position. I want to change it so that every new sprite takes a different position of the other two. I am not really getting it right. Please help.
- (float)randomlyChooseXValue {
CGSize s = [[CCDirector sharedDirector] winSize];
int randX = arc4random() % 3;
if (oldRandX != randX) {
if (randX == 0) {
xPos = xPos1*(s.width/480.0);
} else if (randX == 1) {
xPos = xPos2*(s.width/480.0);
} else {
xPos = xPos3*(s.width/480.0);
}
oldRandX = randX;
} else {
[self randomlyChooseXValue];
}
return xPos;
}
If I understand this correctly you need to find 3 random values and the 3rd one should be different then the 1st 2. If this is true you need to store last 2 random values and compare them in the 1st if statement:
- (float)randomlyChooseXValue {
CGSize s = [[CCDirector sharedDirector] winSize];
int randX = arc4random() % 3;
if ((oldRandX1 != randX) && (oldRandX2 != randX)) { //check for 2 values
if (randX == 0) {
xPos = xPos1*(s.width/480.0);
} else if (randX == 1) {
xPos = xPos2*(s.width/480.0);
} else {
xPos = xPos3*(s.width/480.0);
}
oldRandX2 = oldRandX1; //store 1st value to 2nd place
oldRandX1 = randX; //store new value to 1st place
} else {
[self randomlyChooseXValue];
}
return xPos;
}
Since you explained that you are okay with the sequence repeating, then you only need to really make two random choices: the first position, and the direction.
// somewhere, initialize global oldRandX = -1, dir = -1
- (float)randomlyChooseXValue {
CGSize s = [[CCDirector sharedDirector] winSize];
if (oldRandX < 0) {
oldRandX = arc4random() % 3;
dir = arc4random() % 2;
} else if (dir) {
oldRandX = (oldRandX + 1) % 3;
} else {
oldRandX = (oldRandX + 2) % 3;
}
if (randX == 0) {
xPos = xPos1*(s.width/480.0);
} else if (randX == 1) {
xPos = xPos2*(s.width/480.0);
} else {
xPos = xPos3*(s.width/480.0);
}
return xPos;
}
This can generate every possible sequence of the three positions:
0, 1, 2
0, 2, 1
1, 2, 0
1, 0, 2
2, 0, 1
2, 1, 0
and repeat them.
The other answer will achieve the same results, but like your original method, it might take several tries to get it. Your random function can keep choosing the wrong value an unbounded number of times; it becomes a worse problem when there's only one right number to pick.

Resources