I want to keep the zero at the beginning of my NSInteger, but when i NSLog it, the zero is removed.
NSInteger myInteger = 05;
NSLog("%d", myInteger);
log: 5
I get 5 instead of 05. How can i keep the 0 at the beginning of the integer?
NSInteger doesn't do "leading" zeros. You're thinking about a number formatting thing.
If you just want to print out leading zeros via "NSLog", try something like:
NSLog( "%02d", myInteger);
Which instructs NSLog to have two digits and if it doesn't reach two digits, do a leading zero.
Take a look at the printf format specifiers (which NSLog tries to conform to) and you'll see how leading zeros are added there.
I am not sure, but I think it is a question of how you write your "%d" flag. So you cas use NSLog(#"%03d", var);.
It will print 005.
This is how it works for strings:
INT
String(format:"%05.2f", 5.0) will output: 05.00
DOUBLE
String(format:"%02d", 5) will output: 05
If you need it just for logging try it like this
NSLog(#"0%d" , 5) ;
or if you need to print it on the screen convert to string with format :
NSString *result = [[NSString alloc]initWithFormat:#"0%d" , yourInteger] ;
Related
I'm trying to get an NSNumber out of an NSMutableArray that's been previously manipluated as a double and then added to the array to print out in a label (NSString).
It's important that the number stays as an accurate representatoin of a double with no scientific notation to abbreviate the answer.
The other requirement is to have it print to maybe 15 or 16 decimal places, rounding is optional but not required.
I also do not want trailing 0's when displaying the double
I've tried the following but these do not work...
This is ok but ends the number with a . (eg: 1+1=2.)
double test = [[data.argOperands objectAtIndex:0]doubleValue];
label.text = [NSString stringWithFormat:#"%3.2f", test];
label.text = [label.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:#"0"]]];
I then try something like this, which is wrong because if I do 9^99 it'll print inf or 0.0003/4 it'll give scientific numbers instead of the value
float y = [[calcData.argOperands objectAtIndex:0]doubleValue];;
label.text = [NSString stringWithFormat:#"%g", y];
If I do the following using double it's getting close, 9^99 works, but 3.33/5 returns 0.666000 with trailing 0's
double y = [[data.argOperands objectAtIndex:0]doubleValue];
label.text = [NSString stringWithFormat:#"%f", y];
Any code examples of how to do it this way using either NSNumberFormatter or NSDecimalNumber would be greatly appreciated.
"%.13f" Would give you 13 decimal places, but that would give you trailing zeros.
You may need to create an NSNumberFormatter and use that.
I suspect you're not going to be happy no matter what. Binary floating point is not an exact representation of decimal. The decimal value .1 is not an exact value in binary, and might display as something like .09999999998 if you display it with 13 decimal places.
You also might look at using NSDecimalNumber, which stores values as decimal digits. It's slower than other ways of doing math but you can control the results exactly.
After looking over http://nshipster.com/nsformatter/ and the giant NSNumberFormatter_Class doc I've come up with this code that prints everything to my requirements:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setUsesSignificantDigits: YES];
numberFormatter.maximumSignificantDigits = 100;
[numberFormatter setGroupingSeparator:#""];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
label.text = [numberFormatter stringFromNumber:stringFromNumber:#(1234.567800000555)];
This will actually print 1234.56780000056 (missing the 12th decimal place and rounding it up to the 11th decimal place) though I'm happy enough with this.
I'm still cleaning up the answer, I don't need maximumSignificantDigits = 100 obviously, but generally having a large number there helps to ensure I'm getting all the decimal places I need.
I had to set setGroupingSeparator:#"" because NSNumberFormatterDecimalStyle puts commas in numbers and I don't want them (eg: Instead of getting 1,000 I want 1000).
in iOS Objetive-C I am trying to get the number typed by the user in a text field to set the upper bounder of a random number generation function in C.
- (IBAction)pushTheButton2:(id)sender {
u_int32_t upperBound = (u_int32_t) textField3.text;
textField4.text = [NSString stringWithFormat:#"%d", arc4random_uniform(upperBound)];
}
The output is a giant number that makes no sense. To test if function works, if I hardcode the actual upper bound in the arc4random_uniform function, such as arc4random_uniform(5), then it works!
I figured this could be some kind of literal conversion, so I tried to make this work with u_int32_t but still not outputting the right range.
Can someone help? Thanks
You are currently taking the memory reference pointer of the text and using that as the upper bound.
Try doing something like this instead...
NSInteger upperBound = [textfield.text intValue];
This will convert the string into an int that you can then use in the arc random function.
To parse string to integer you should do:
NSInteger upperBound = [textfield.text integerValue];
I have app with three view controllers. First is UIViewController with three UITextField's where user put some digits. These digits are stored to my CoreData entity as String attributes.
Second is UITableView, where I show my stored data from CoreData as new cell.
Third is detail UIViewController where I show user all his previously inserted digits.
The problem is when I set on textField decimal pad, user have digits and comma but my function need digits with dot for double precision calculating.
With comma I can't make mathematical function as [.....] * [...] = .... because it doesn't work.
Any idea how I can figure it out?
Can I simply change that comma to a dot?
I have this code:
NSNumberFormatter*nf = [[NSNumberFormatter alloc]init];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
double result = [myString doubleValue] * [myOtherString doubleValue];
You want the decimal keypad to have either a comma or a period (dot) based on the user's locale. This is so the user can enter the value as they are accustomed to. In order for you to do the math with the vales, you need to use a NSNumberFormatter to convert the entered string into a double value. Once you do this, your math formulas will work. Never use doubleValue or floatValue to convert an NSString to a double or float if the string was entered by the user. Always use NSNumberFormatter to properly deal with the user's locale.
Update based on code added to question.
You don't use the number formatter. You are doing exactly what I said not to do. Change your code to:
double firstValue = [[nf numberFromString:myString] doubleValue];
double secondValue = [[nf numberFromString:myOtherString] doubleValue];
double result = firstValue * secondValue;
Solution is very simple:
1. Go to Settings > General > International > Regional Format
2. Now select "United States"
Now run your App and see dot(.) instead of comma(,) in Decimal pad keyboard.
I have a statement that shows 50.02%
How can I drop the decimals and show 50%?
Sorry this is so basic, pretty new to xcode.
Thank you in advance.
-(void)updatePercent{
percent.text = [NSString stringWithFormat:#"%.2f%#", 100.0f, #"%"];
}
Use this:
percent.text = [NSString stringWithFormat:#"%.0f%%", 100.0f];
Also note the use of %% to print a single %.
When printing floats, you can control how many decimals places are printed out by using numbers between the % and the f in the %f format statement.
The number on the right of the decimal tells how many decimal places should be printed. The number on the left of the decimal tells at least how many total places should be printed (including the decimal and all digits). If the number on the left is prefixed by a zero, the resulting string will have zeros appended to the left.
float myNum = 32.142;
// without declaring total digits
[NSString stringWithFormat:#"%.0f"] // 32
[NSString stringWithFormat:#"%.2f"] // 32.14
[NSString stringWithFormat:#"%.5f"] // 32.14200
// put a zero in front of the left digit
[NSString stringWithFormat:#"%010.0f"] // 0000000032
[NSString stringWithFormat:#"%010.2%"] // 0000032.14
// without the zero prefix (leading whitespace)
[NSString stringWithFormat:#"%10.2f"] // 32.14
is there any way to put out the alphabet in iOS?
I want to populate a table with all the letters of an alphabet.
And also other alphabets.
Any suggestion to do that without any extensions or smth. else ??
If any way to do so with an extension, please let me know :)
Edit 1:
Thank you Carlos Chiari, here is my solution:
int asciiCode = 65;
for (asciiCode=65; asciiCode<=90; asciiCode++) {
NSString *string = [NSString stringWithFormat:#"%c", asciiCode];
NSLog(#"%#", string);
}
found here and just putted in a for condition:
How to convert ASCII value to a character in Objective-C?
Not sure about the exact syntax since I haven't played with iOS in a while, but just loop through the different ASCII characters from A - Z.
For instance in .Net it would be something like
For i as Integer = 65 To 90
Label.Text &= Char(i)
Next i
Char(i) prints the character with ASCII code "i", ASCII code for capital A is 65. ASCII code for capital Z is 90. So i loop through it from 65 to 90, which gives me A-Z.
Hope this helps :)