Limit Characters in Double - iphone

How can I make a double appear with only two decimal places? Current code is as follows (but of course, it's showing a lot of decimal places):
tipPercentDbl = 20;//tipPercent as Dbl
tipDbl = (totalDbl * (tipPercentDbl/100));//tip as Dbl
tip.text = [NSString stringWithFormat:#"%f", tipDbl];
tipPercent.text = [NSString stringWithFormat:#"%f", tipPercentDbl];
totalWithTipDbl = (totalDbl+tipDbl);
totalWithTip.text = [NSString stringWithFormat:#"%f", totalWithTipDbl];

Change this
tipPercent.text = [NSString stringWithFormat:#"%f", tipPercentDbl];
to this
tipPercent.text = [NSString stringWithFormat:#"%.2f", tipPercentDbl];

You want %.2f:
tip.text = [NSString stringWithFormat:#"%.2f", tipDbl];
tipPercent.text = [NSString stringWithFormat:#"%.2f", tipPercentDbl];
// ...
totalWithTip.text = [NSString stringWithFormat:#"%.2f", totalWithTipDbl];
Format specifiers take the form of:
%[flags][width][.precision][length]specifier
where .precision in the case of f specifiers means the number of digits to be printed after the decimal point.

Related

Getting substring from response NSString

I need to get substring CODE's value (X2.31) from string
NSString *str = #"SHMU=\"\" CODE=\"X2.31\" XTN=\";
How could I get that particular substring?
Try the below one
NSString *str = #"SHMU=\"\" CODE=\"X2.31\" XTN=\"";
NSRange range = [str rangeOfString:#"CODE="];
NSString *substring = [[str substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *str1 = [substring componentsSeparatedByString:#" "];
NSLog(#"the sub %#",[[str1 objectAtIndex:0] stringByTrimmingCharactersInSet[NSCharacterSet whitespaceCharacterSet]]);
And by string was "X2.31"
NSString *strOrg = [NSString stringWithFormat:#"%#",#"SHMU=\"\" CODE=\"X2.31\" XTN=\""] ;
NSString *strLeft = [NSString stringWithFormat:#"%#",#"SHMU=\"\" CODE=\""] ;
NSString *strRight = [NSString stringWithFormat:#"%#",#"\" XTN=\""] ;
NSLog(#"%#", [self getDataBetweenString:strOrg LeftString:strLeft RightString:strRight LeftOffset:13]);
- (NSString *)getDataBetweenString:(NSString *)orgString LeftString:(NSString *)leftString RightString:(NSString *)rightString LeftOffset:(NSInteger)leftPos;
{
NSInteger left, right;
NSString *foundData;
NSScanner *scanner=[NSScanner scannerWithString:orgString];
[scanner scanUpToString:leftString intoString: nil];
left = [scanner scanLocation];
[scanner setScanLocation:left + leftPos];
[scanner scanUpToString:rightString intoString: nil];
right = [scanner scanLocation] + 1;
left += leftPos;
foundData = [orgString substringWithRange: NSMakeRange(left, (right - left) - 1)]; return foundData;
}
This is only specific to the str you posted.
Your number must be followed by First X.
float f=[[str componentsSeparatedByString:#"X"][1] floatValue];

Add character or special character in NString?

I have an NSString *string=#"606" and I want to add a ":" colon after two digits.
The output should look like this: 6:06
It this is possible?
Help would be appropriated.
Thank you very much.
You can add the column between the hours and the minutes like this:
NSString *string = #"606";
NSString *result = [string stringByReplacingCharactersInRange:NSMakeRange(string.length-2, 0) withString:#":"];
NSLog(#"%#", result);
This will give the following results
#"606" => #"6:06"
#"1200" => #"12:00"
#"1406" => #"14:30"
Note: This will only work if the string has 3 or 4 characters, but this is the case according to your question.
Because you do not have two separate objects for hours and minutes, use:
NSString *newTimeString, *hour, *minute;
NSUInteger length = [timeString length];
if (length == 3)
{
hour = [timeString substringToIndex:1];
minute = [timeString substringFromIndex:2];
}
else
{
hour = [timeString substringToIndex:2];
minute = [timeString substringFromIndex:3];
}
newTimeString = [NSString stringWithFormat:#"%#:%#", hour, minute];
I used a long version to illustrate the concept. Basically, use the length of the original string (timeString) to extract the time components and combine them with a colon.
Will it be 2 digits no matter what? Otherwise you could build your custom string from parameters.
NSString *string = [NSString stringWithFormat:#"%#:%#", hours, minutes];
UPDATE
If you just need to add a colon after 1 char, you can do it this way. Although I would suggest finding a safer method as this could be inaccurate if you have a double digit hour.
NSString *hour = [NSString substringToIndex:1]; // double check the index
NSString *min = [NSString substringFromIndex:1]; // double check the index
NSString *time = [NSString stringWithFormat:#"%#:%#", hour, min];
Hopefully this will help.

String to double

I hope you can help me out with this 'small' problem. I want to convert a string to a double/float.
NSString *stringValue = #"1235";
priceLabel.text = [NSString stringWithFormat:#"%d",[stringValue doubleValue]/(double)100.00];
I was hoping this to set the priceLabel to 12,35 but I get some weird long string meaning nothing to me.
I have tried:
priceLabel.text = [NSString stringWithFormat:#"%d",[stringValue intValue]/(double)100.00];
priceLabel.text = [NSString stringWithFormat:#"%d",[stringValue doubleValue]/100];
but all without success.
This is how to convert an NSString to a double
double myDouble = [myString doubleValue];
You have to use %f to show float/double value.
then %.2f means 2digits after dot
NSString *stringValue = #"1235";
NSString *str = [NSString stringWithFormat:#"%.2f",[stringValue doubleValue]/(double)100.00];
NSLog(#"str : %# \n\n",s);
priceLabel.text = str;
OUTPUT:
str : 12.35
I think you have the wrong format string. Where you have:
[NSString stringWithFormat:#"%d", ...];
You should really have:
[NSString stringWithFormat:#"%f", ...];
%d is used for integer values. But you're trying to display a floating point number (%f).

iPhone NSNumberFormatter for less than 1 value

I have a floating point value
i.e. 0.0467
Want have a string
05
how can get it? Excluding the decimal point (.).
More precisely, if I have a floating point number, I want to divide it to integral and decimal, preferably into two string parts.
By following this, you will get desired result.
float floatValue = 0.0467;
NSString *str = [NSString stringWithFormat:#"%.2f", floatValue];
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:#""];
NSLog(#"%#", str); // Result will be: 05
fDecimal = 0.04567;
NSString * strDecimal = [NSString stringWithFormat:#"%0.2f", fDecimal];
NSString * strDecimalPart = [strDecimal substringWithRange:NSMakeRange(2, 2)];
The setting you are looking for is called fraction digits:
NSNumberFormatter* f = [[[NSNumberFormatter alloc] init] autorelease;
[f setMaximumFractionDigits:2];
Optionally you can use -[NSNumberFormatter setRoundingMode:] to specify how rounding should be done.

convert a char to string

my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:#"%#", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:#"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(#"%c", extractedChar);
}
You should be able to use -stringWithFormat:.
NSString *s = [NSString stringWithFormat:#"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:#"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog(), see what it exactly hold, you code seem correct,
Use characterAtIndex function for NSString to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:1]];