How do I set the fill character for displaying numbers (Objective C) - iphone

I'm trying to format a string to give me 2 characters for a number, no matter what its value. Right now, I have
[NSString stringWithFormat:#"%2d:%2d:%2d",h,m,s)];
and for the values 1, 2, 3, the output is
1: 2: 3
How do I change the spaces to 0's ?

This is done the same as C's printf (see man 3 printf):
[NSString stringWithFormat:#"%02d:%02d:%02d",h,m,s];
(By the way, if you're trying to format dates or times, I'd suggest looking at NSDateFormatter.)

Related

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.

Remove commas and decimal places from number field

I am trying to add two zero place holders in front of a field without changing the actual values involved. The field is an order number that is being pulled from MOMs. So right now that fields' formula is {cms.ORDERNO}.
When I try '00'+{cms.ORDERNO} the field displays 001,254.00. How can I remove the decimals and comma so it displays 001254?
The usual trick is to pad with plenty of extra digits on the left and then only take the six you really want from the right. This would handle any order number ranging from 1 to 999999.
right("000000" + totext({cms.ORDERNO}, "0"), 6)
When you don't specify a format string, as you tried, it uses default settings which usually come from Windows. By the way, if I recall correctly cstr() and totext() are equivalent for the most part but totext() has more options.
You should also be able to specify "000000" as the format string to produce the left-padded zeroes. Sadly I don't have Crystal Reports installed or I'd check it out for you to be sure. If this is the case then you probably don't need a formula if you just want to use the formatting options for the field on the canvas. If you do use a formula it's still simple.
totext({cms.ORDERNO}, "000000")
You definitely want to use the Replace formula a few times for this. The formula below converts ORDERNO into string, removes any commas and trailing decimal places, then adds the two zeroes at the beginning:
`00` + REPLACE(REPLACE(CSTR({cms.ORDERNO}),".00",""),",","")
So for example, if cms.ORDERNO is 1,254.00 the output from this formula would be 001254
I know this is older, but better solutions exists and I ran across this same issue. ToText has what you need built right in.
"00" + ToText({cms.ORDERNO}, 0, "")
From the Crystal Documentation:
ToText (x, y, z)
x is a Number or Currency value to be converted into a text string; it
can be a whole or fractional value.
y is a whole number indicating the number of decimal places to carry
the value in x to (This argument is optional.).
z is a single character text string indicating the character to be
used to separate thousands in x. Default is the character specified in
your International or Regional settings control panel. (This argument
is optional.)

Matlab read one digit at a time from text file

I have a file that contains byte values 0 or 1 that are formatted without any whitespace between, like 1010111101010010010101. I want to make a [1, 0, 1, ...] vector out of those, reading one digit at a time. How can I do that? I tried using fscanf(fileId,'%c') but I get ASCII codes instead of actual values. '%d' on the other hand reads the entire file as one number.
I also tried writing to file:
fprintf(file1,'%d ',matrix); //notice the space after `%d`
and reading
fscanf(file2,'%d');
but I get a Nx1 matrix and I want to keep it as 1xN.
I could transpose it to be horizontal, but I still need to add space between digits, and I don't want to do that if possible.
You can convert easily from ascii char code to integer format as follows:
text = fscanf(fileId,'%c') - '0' ;
Note that you will also pick up end-of-line characters this way if there are any.
If you only have 0/1 in your file, using fileread will accomplish the same thing but also catches EOL characters:
text = fileread('test.txt');
text = text' - '0';
You can also read the entire file with textread:
text = textread('test.txt','%s');
text = char(text) - '0' ;
Now lines are returned in a cell array with one row per line. char then converts the cell array to a regular char array. This will not capture EOL characters but char will append blank spaces (ascii code 32) if the lines are not all equal in length.
Finally, you can also read line by line by looping and applying fgetl at each iteration until the function returns a -1.
while ~isnumeric(c)
c = fscanf(fileId,'%c')
c - '0';
end
This avoids reading EOL characters and appending blank space but you need to handle catenating the data.

How to create a "0000001" type number format?

I want to generate incremental receipt numbers like this format "00000001". Every time new transaction happen, receipt number should increase by 1.
0000001
0000002
0000003
0000010
0000011
0000100
0000101
So, how could I implement this type of number format. Is there any special number formats in objective C?
If your numbers are integers and only the output matters, you can do:
NSString * output = [NSString stringWithFormat:#"%07d", integer];
to have the number be formatted with 7 digits, leading zeroes.
How about just answering the question
NSString *str=[NSString stringWithFormat:#"%08d", intvalue];
change the 8 to however many leading zeros you want

printing int with specified nr of digits

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