printing int with specified nr of digits - iphone

I am trying to print out a 5 digit number as follows:
int rdmNr = arc4random()%100000;
NSLog(#"%i",rdmNr);
I always would like to have 5 digit numbers. Example outputs should be:
00001
10544
00555
78801
But with the previous code I would also get 1 or 555 without 0s. I also tried %5i, but the I just get more white spaces.

try
NSLog(#"%05i",rdmNr);
In general, you can specify 2 types of padding for NSLog - filling the required extra characters with spaces (#"%5i") or with leading zeros (#"%05i")

NSLog(#"%05i", rdmNr);
The 5 means that 5 characters should be printed, the 0 means that it should be padded with zeros to the left.
NSLog accepts the same arguments as printf (see here) in addition %# prints an NSString, more info here.

You can also find some common specifiers at Placeholders on Wikipedia

Related

How to fix extra space that MATLAB displays after first iteration

I have an fprintf statement which loops 3 times in order to display some data. After the first iteration, MATLAB displays a mysterious space even though I have not added an extra \t. It acts as if I had an if statement to display a different fprintf statement after the first iteration, but I have nothing like that on the code. See picture on the link for the result it displays
% Display results
fprintf('Panel\tPressure Cl\tCd\t| Panel\tPressure Cl\tCd\n')
for q = 1:length(AOA)
fprintf('--------------\t-------\t------- |--------------\t--
-----\t-------\n')
fprintf('AOA %.0f°\t\t%.4f\t%.4f\t|AOA %.0f°
\t\t%.4f\t%.4f\n'...
,AOA(q),Cl(q),CD(q),AOA(q),ClFinal(q),CDFinal(q))
fprintf('--------------\t-------\t------- |--------------\t--
-----\t-------\n')
for j = 1:length(pressure{1})
fprintf('%.0f\t%.4f\t |\t |\t|%.0f\t%.4f\n',j+1,pressure{q}
(j),j+1,pFinal{q}(j))
end
end
When you fprintf a \t character, there is an automatic space padding up to 4 spaces. If the string has less than 4 characters, the string will be placed at the start and be "space padded" until 4 characters have been filled (in reality, the space padded characters resemble just one character). If the string has more than 4 characters, then it will space pad at 8, 12, 16, etc...
Here is what your question is really about:
fprintf('Panel\tPressure Cl\tCd\t| Panel\tPressure Cl\tCd\n')
The first string Panel has 5 characters, and therefore will be space padded with the equivalent of 3 spaces at the end of the first Panel. However, the second string | Panel has 7 characters, and therefore will only need the equivalent of 1 space at the end of the second string.
To remove your spacing issue, and have a more uniform spacing between your text headers, you can place a tab character after every header you want, and change your formating for your other fprintf statements accordingly:
fprintf('Panel\tPressure\tCl\t\tCd\t\t|\tPanel\tPressure\tCl\t\tCd\n')
You can also view this link for another example of how space padding works.
Also, here is the MATLAB Documentation on Formatting Text.

printf number format for constant width lat/long in exiftool

Apparently I am misunderstanding the printf man page. (Or else it's a bug in exiftool 10.55 and 10.77)
I am trying to get GPS coordinates from image files with exiftool. I would like to make them the same width and without unnecessary spaces.
The format string I tried, and one of the results:
-coordFormat "%03d°%02d′%0d%02.5f″"
042°37′280.00000″ N, 002°05′510.00000″ W
(I don't need five decimal places—I just put that in temporarily to see whether any of the cameras wer being dishonest about the precision.) The three unnecessary spaces can't be helped; they are outside the format string’s control, but I did get rid of others that were in the default.  The leading zero for latitude isn't needed, but it is there because longitude uses the same format string.  One problem is the bogus zero inserted between floor(seconds) and its decimal point. The other problem is the false fractional part.  The default format for that file is 42 deg 37' 28.39" N, 2 deg 5' 51.96" W
Someone's "cheat sheet" said that my second digit should be the total width, including the decimal point, so I changed the seconds to "%08.5f" but all that did was add another bogus zero in front of the decimal point, e.g., 510.00000→5100.00000 (width of ten, not eight!).
A few years ago, I did something similar, and got the correct results.  But I didn't bother to save the script "for future reference."
(Several other SO answers agree with that "cheat sheet.")
It looks like the issue is with the seconds field, for which you have the format specifier %0d%02.5f. I'm not sure what you intended, but there can be only one % for each value to be rendered
If you're formatting longitude then you are dealing with values between -180 and 180. If you want five decimal points then the total width will be
One character for the sign + or -
Three characters for the integer part
One character for the decimal point .
Five fractional digits
giving a total field width of ten. Your full specifier will be %0+10.5f, giving output between «-180.00000» and «+180.00000»
You may use a space flag instead of the +, as in %0 10.5f, which will use a space instead of a + to indicate a positive number, rendering 180 as « 180.00000». The leading zero is there so that zeroes are use to fill the full ten character field with
When dealing with latitude, you will need a total width one character smaller. %0+9.5f will result in a range of «-90.00000» to «+90.00000». Of course you may use the same format specifier as for longitude, which will produce «-090.00000» to «+090.00000». This way the latitude and longitude seconds will have the same number of characters
The %0d is throwing you off. That part of the template is consuming the "51.0" seconds component of the coordinate, leaving nothing for the %02.5d part of the template.
printf "%0d", 51 ===> "51"
printf "%02.5f"; ===> "0.00000"
printf "%0d%02.5f", 51 ===> "510.00000"
So lose the %0d.
The 2 in %02.5f also doesn't do you any good. The number before the decimal place is the minimum length of the field, and the number after the decimal place is the number of decimal places to use. Since 5 decimal places will be printed, the output will be at least 7 characters, and the 2 value will be ignored.
First number is the width, second number is the number of decimal points so what you have currently (%2.5f) appears to be backwards. %5.2f would give you a number that occupies 5 characters and has 2 decimal places. For a number as big as 510, you probably want to make it %6.2f

How can i get 6 digits after comma (matlab)?

I read from text some comma seperated values.
-8.618643,41.141412
-8.639847,41.159826
...
I write script below;
get_in = zeros(lendata,2);
nums = str2num(line); % auto comma seperation.(two points)
for x=1:2
get_in(i,x)=nums(x);
end
it automatically round numbers. For example;
(first row convert to "-8.6186 , 41.1414")
How can i ignore round operation?
I want to get 6 digits after comma.
I tried "str2double" after split line with comma delimeter.
I tried import data tool
But it always rounded to 4 digits, too.
As one of the replies has already said, the values aren't actually rounded, just the displayed values (for ease of reading them). As suggested, if you just enter 'format long' into the command window that should help.
The following link might help with displaying individual values to certain decimal places though: https://uk.mathworks.com/matlabcentral/newsreader/view_thread/118222
It suggests using the sprintf function. For example sprintf(%4.6,data) would display the value of 'data' to 6 decimal places.

format floating points in matlab using fprintf function

Consider the following code:
A1 = [9.9, 9900];
A2 = [8.8, 7.7 ; ...
8800, 7700];
formatSpec = 'X is %4.2f meters or %8.3f mm\n';
fprintf(formatSpec, A1, A2)
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm
I would like to know what does 4.2f or 8.3f mean in this case? Does it means how many digit we should use after .?
For instance by looking on code, it seems for me difficult to understand what they mean, while .2 or .3 appears a bit clear, first digit 4 and 8 became difficult to interpret, if it is related to mantissa and exponent, then why do we need it there?
Please help me to clarify such things
The first number indicates the total number of character spaces (including the delimiting .) the number will take up when printed. The second - as you pointed out - represents the number of decimals.
For example, if you print 1.2 with 8.3f you get three empty spaces before the number:
1.200
12345678 characters total
If you were to use 5.2f your output would be.
1.20
12345 characters total
The second line was added by me to illustrate the total number of characters (including white space). It is not part of the original output
Edit
In your example, using 8.3f for 1.2 wouldn't make much sense. However, if you wanted to write lots of column data to a file that could easily be read by another program, this might be more useful (because the format could be known). E.g. Consider two columns %8.3f%8.3f (note how you do not need a space between the floating point number formatter). This could give you an output like this:
1.200 34.564
8503.000 101.008
... and so on so forth. Here, the leading blank space helps. It will fail when you have numbers above 9999.999 in this case.
Edit 2
In Matlab, if you specify a number of total characters that is less than the number of digits you have before the decimal point (or none at all), it will just print the entire number. E.g. using %2.3f will give you
1.200
with no leading white spaces. If you only cared about the decimals printed, you could also use %.3f which again results in
1.200

Notepad++ find and replace number, digit format

I have single and two digit number, and I want to make it 4 digits.
So "1" become "0001", "22" become "0022"
How do I do that?
You have to do two replacements:
search: \b(\d\d)\b
replace: 00$1
and:
search: \b(\d)\b
replace: 000$1
Generalising the question. Suppose we want to convert a collection of numbers so that all have, say, 7 digits by adding leading zeroes as necessary. This can be done in two steps.
Add 7 leading zeroes to each number, so eg 2 33 456 789012 becomes 00000002 000000033 0000000456 0000000789012.
Convert each number to have the required number of digits by removing some leading zeroes to leave the wanted 7 digits.
In more detail.
Search for \b(\d{1,6})\b which finds numbers with between 1 and 6 digits inclusive. Replace them with 0000000\1. There is no need to search for 7-digit numbers as they are already the correct length.
Search for \b0+(\d{7})\b and replace with \1.
Notes
Input numbers that have more than 7 digits will not be found by step 1.
Input numbers that have leading zeros with less than 7 significant digits, e.g. 001234 will have the 7 zeroes added by step 1 whereas 00000000000001234 is longer than 7 digits and so will not be changed by step 1.
Input numbers with leading zeroes and more than 7 significant digits will not be changed.
I used \1 instead of $1
First Replacement:
search: (\d\d)
replace: 00\1
Second Replacement:
search: (\d)
replace: 000\1