I have a float number that could be 3.553 or 34.535 or 353.6436 I want it to be no more than 5 figures, before or after the dot, for example, for the first number I wrote it has to be 3.5530, for the second 34.535 as is and for the third 353.64.
How can I do that with NSNumberFormatter?
Use NSNumberFormatter to convert the number to a string. Specify 5 decimal digits and turn off any grouping. Once you have the string, get the index of the decimal separator. If its index is 4 or less (a number less than 10,000), take the first 6 characters of the string. If its index is 5 or more (a number greater or equal to 10,000), take the first 5 characters of the string.
You may have to adjust this depending on how you want to deal with numbers less than 1 due to the leading zero ahead of the decimal separator.
Related
In Google Sheets, I can use the ROUND function to round to the nearest decimal.
5.1 = 5
5.5 = 6
What if I want like this:
5.1 = 6
5.5 = 6
What function should I use?
ROUNDUP is exactly what you need here.
Rounds a number to a certain number of decimal places, always rounding up to the next valid increment.
BJ190215P00020000
this is the string. I want to extract the left letters "BJ" (they are from 1 letter up to letters).
I think i need to find the position of the first digit, then apply LEFT function to get out the "BJ". (the letters could be 1-8).
is there a way to find the position of first digit in the string. OR, is there a better way to extract the letters before the first digit?
Try ...
=regexextract(A1,"\D+")
I have a 5x5 grid of tiles that are number like:
Numerical order Row number(eg: 1 1 would be the first tile on the first row and 6 2 would be the first tile on the second row)
I need to get the blocks around a clicked tile (above, below, left and right), I thought about doing this by taking off numbers from the numerical order and row number. I wrote this:
local ab = tostring(tonumber(v.Name)-5)..tostring(tonumber(string.sub(v.Name,-1))-1)
local be = tostring(tonumber(v.Name)+5)..tostring(tonumber(string.sub(v.Name,-1))+1)
ab being the tile above and be being the tile below. I ran into a problem where I cannot get the first two letters of a tile who's numerical order is two digits using one line (I don't want to use if statements since I'm pretty sure there's a one line solution)
I came up with a solution and that is to get all the characters before the whitespace (which separates the order from the row number) but I have no idea how to write it.
Just ask for all non-whitespace characters from the beginning of the string:
print(("test123 more456"):match("^(%S+)"))
This should print test123.
I have a picker view that displays output in a textfield. It is for the user to select an amount of time, it has hours, minutes, and seconds for the 3 rows. For this part I only am focusing on the seconds. I want to be able to convert the hours to minutes and display an output as a whole number and a decimal. I was able to convert the hours to minutes, but I am having trouble converting the seconds into a decimal. It should be as simple as dividing the row count by 60. However, I can't get that to display right. The output to the label is just 0.00 no matter what row i select.. Here is my code.
float sec;
sec = ([timePickerView selectedRowInComponent:2]/60);
Label.text = [NSString stringWithFormat:#"%.2f",sec];
The "/" sign in relation to the number "60" is considered an integer division by the compiler. Change "60" to "60.0" and you shall get the result you are expecting. You should also make sure that the [timePickerView selectedRowInComponent:2] returns a float by casting it:
(float)[timePickerView selectedRowInComponent:2]
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
cobol difference with Picture having a dash (-) and a having a X
I'm tying to get to grips with Cobol and can't understand the dashes when formatting a number. I have this example:
--9
Am I correct with the following?
The first dash - If number is a negative put a dash otherwise don't.
the second dash - I'm confused with this. There is already a dash at the start to specify whether its negative or positive.
9 - Numeric digit (0-9)
An example would be good. :S
Thanks
In view of your previous question, Im not sure what you are having trouble with. But lets try again...
In COBOL, numeric display fields may contain various types of "punctuation". This "punctuation" is defined in the items PICTURE clause. A few examples of the type of "punctuation" symbols you can use are: Explicit decimal points, plus/minus signs, CR/DR indicators and thousnads separators (commas in North America). There is a well defined set of rules that determine what type of "punctuation" can occur in the PICTURE clause and where. This link to PICTURE CLAUSE editing explains how to construct (or read) any given PICTURE clause.
One thing that you, and many others new to COBOL, trip up on is that a data definition in COBOL specifies two distinctly different types of information about numeric display data. One is the range of values it may hold and the other is how
that range of values may be displayed. Your example: PICTURE --9 tells me two things about the data item: 1) Values are integers in the range of -99 through to +99, and 2) Displaying this item will take 3 spaces. If the number is positive, spaces will appear before the first non zero digit. If the number is negative a minus sign will appear immediately to the left of the first non zero digit. Consider the following COBOL DISPLAY statement:
DISPLAY '>' DISP-NBR '<'
IF DISP-NBR has a PICTURE clause of: --9 this is how various values will be displayed.
0 displays as: > 0<
-1 displays as: > -1<
-11 displays as: >-11<
10 displays as: > 10<
Note that all displays take 3 character positions. At least 1 digit will always be displayed (because of the '9' in the PICTURE clause), other than that, no leading zeros are displayed. A minus sign will display only for negative values. The minus sign, if displayed will be to the immediate left of the first displayed digit.
Now to answer you specific question: The total number of character positions needed to display a numeric display data item is determined by the length of the PICTURE. You have a 3 character PICTURE so 3 character positions are needed. When
a sign is specified in the PICTURE, a space is always reserved for it. This is what limits the range of integers to those containing at most 2 digits. The second minus sign indicates 'zero supression'. Zero supression just means not printing leading zeros. Only 1 minus sign is ever printed and it will be to the immediate left of the first displayed digit.
COBOL contains a lot of flexability with respect to displaying numbers. Understanding the numeric display PICTURE clause is key to understanding how this all works.
from stackoverflow:cobol-difference-with-picture-having-a-dash-and-a-having-a-x
The dash means that if you have a negative number, a dash will be
shown beside (at the left) of the number. Only one dash will be
displayed. If the number is positive, a space will shown for every
dashes.