% format %2s 100
100
% format %.2s 100
10
%
%
% format %0.2s 100
10
%
I am not able to understand the difference between %2s and %.2s .
Can anyone explain me ?
TCL format command manual page specifies that format string can consist of six different parts. In this case second, third and fourth portions are of interest.
If there is a character from set [-+ 0#], they specify justification of the field, if there should be padding, sign shown of numbers, etc. 0 in the third example specifies that number should be padded with zeros instead of spaces. However, in this example there is nothing to pad.
If there is some other number without dot (2 in the first example), the number is interpreted as minimum field length and number is padded with spaces if necessary.
If there is a dot, the number after if interpreted as precision indicator and way it behaves differs depending on the other format parameters. For strings it means the maximum number of characters.
With
format %4.2s foo
you then get
fo
That is, at most two characters are printed, but the field width is at minimum 4 characters.
If you are actually trying to print a number instead of string, then the sixth (the only mandatory) field is important. "s" means "print as is". For numbers you want to use for example "d" which means decimal (integer) or "f" for floating point. Check the manual for the whole list.
With
format %4.2d 100 # Print with at least two numbers and with field width of 4 characters
you get
100
With
format %08.2f 123.45678 # Field width 8, pad with zeros, print two decimals
you get
00123.46
In the last example notice that all numbers and the dot are counted for the field length and that the number has been rounded.
Related
I'm working with a csv file from a customer, which holds a large amount of data. The data is extracted from an SQL database and the commas therefore signify the different columns. In one of these columns there are 10 digit numbers. For some reason all 10 digit numbers starting with 0 have been converted to 9 digit numbers with the 0 removed. I need to find all these instances and insert a 0 at the beginning of the 9 digit number.
A complication in the data is that another column also contains 9 digit numbers, and these do not need to be modified. I can assume, however that all those numbers start with 0 and all the numbers i need to find do not start with 0.
I'm currently using notepad++ trying to fix the problem and found the regular expression \d{9} which finds all numbers with 9 digits, but that is not what I'm looking for
Below i have an example of how the data could look. The column that needs all 9 digit numbers converted is on the left, and the other column with 9 digit numbers is on the right.
An example of the data that is causing the trouble could be:
Column 1
Column 2
2323232323
002132413
231985313
004542435
In this example I need to find the second line of column 1 and insert a 0 in front of the number.
Ctrl+H
Find what: \b(?!0)\d{9}\b
Replace with: 0$0
TICK Wrap around
SELECT Regular expression
Replace all
Explanation:
\b # word boundary, make sure ae haven't digit before
(?!0) # negative lookahead, make sure the next character is not 0
\d{9} # 9 digits
\b # word boundary, make sure ae haven't digit after
Replacement:
0 # 0 to be inserted
$0 # the whole match (i.e. 9 digts)
Screenshot (before):
Screenshot (after):
Using Notepad++ do CTRL + H (search and replace utility).
Tick Regular Expression
Find what ? ([^0-9])(\d{9})([^0-9])
Replace with ? \10\2\3
Explanation :
([^0-9])(\d{9})([^0-9]) matches a 9 digit number surrounded by a non-digit on each side (including line return / comma, etc) :
Each (....) "captures" a group for later use (in "replace").
[^0-9] is a non-number character
\d{9} is a 9 digits number
\10\2\3 is a 0 right after the first captured group \1 (it was just one character here) followed by the 9 digit number (2nd captured group : \2) and the character that was after that number (3rd captured group : \3).
Limit :
It won't match a number at the very beginning of the file (before any other character) or at the very end (after every character). Adding a newline at the end of the file is one workaround, or fixing the last number manually if there is no newline before EOF.
In the following, float(x,y) means a number with x total digits and y decimals.
I am trying to do client side validation for an HTML Input field. The field corresponds to a MySQL column with data type float(x,y). I know I can define a pattern like float(5,2) with lots of 'ORs'. Is there an efficient way of generating a Regex for this such that I can encode it in my web document?
Something of a workaround is to specify \d+(\.\d{1,y})? and then set maxlength=x+1. Should be x+1 because the decimal place is counted. This would then allow the submit of an integer of length x+1 contrary to specification. I realize I can do JavaScript validation but I would like to achieve the desired with HTML validation.
You can first check the total length with a lookahead and then check for the length of numbers after the decimal point.
If you want X total number of digits and at most Y decimal points, you can use:
^(?=.{X+1}$)([1-9]\d*|0)\.\d{1,Y}$
Explanation:
^ asserts you are in the start position of the line
(?= lookahead (zero length) match for:
.{X + 1} X+1 characters
$ end of line //now you have asserted the total length
the whole part either
0 is a single zero
[1-9]\d* more than a single digit and does not start with zero
\. a dot for the decimal point
\d{1,Y} at least 1 and at most Y digits
$ asserts end of line
Note that you do not need to check the length of the whole part since you are already checking for the total length and the length of digits after the decimal so the part before the decimal point is automatically correct.
Example:
For X = 5 and Y = 2, you will have:
^(?=.{8}$)([1-9]\d*|0)\.\d{1,2}$
Regex101 demo
I need a regular expression that matches the following to be used on a web page:
-The first number must be a digit
-The length must be a minimum of 10 digits
-No spaces, alpha, special characters allowed
As my understanding, you need to check if it is a number (containing only the digits 0-9) and this number should be minimum of 10 digits.
Then the following code should work:
/^\d{10,}$/
I have a float column and I'm trying to save the value 1000000. It automatically turns it to 1e+06. How can I fix it?
To have the value returned formatted as 1000000, you can simply add integer zero to the column in the SELECT list.
SELECT mycol+0 AS mycol FROM mytable
MySQL is storing the value IEEE floating point format. (One bit for sign, a certain number of bits for the exponent, and a certain number of bits for the mantissa. This isn't really a MySQL thing, it's the standard representation for floating point values.)
As far as what's being returned, that's an issue with converting that value into string representation.
A floating point number has a large range of values. To represent the maximum value of a float (3.402823e+38) as a decimal value, that would require 38 decimal digits. The seven left most digits of the value are significant, but we'd need to add another 32 zeros/digits to indicate the position of the decimal point.
So, returning a string representation of scientific notation is a reasonable approach to returning a representation of the value.
Those two things are equivalent:
1e+06
= 1 * 10^6
= 1 * 1,000,000
= 1,000,000
It's called scientific notation (see here). mySQL uses it to display huge/tiny values, especially approximate values (see here).
You can use DOUBLE(8, 3) where 8 is the total no. of digits excluding the decimal point, and 3 is the no. of digits to follow the decimal.
I got a numeric column in my database for storing discount percentages for my items.
If the discount is 25,6 it will remove the 6.
What kind of type does this column have to be to not remove the number behind ,?
And what is best practice?
I think you're talking about the decimal data type.
The decimal definition is split into two parts...
the first is the TOTAL number of digits that you want to store, both to the left and the right of the decimal character (normally . or ,). This is called the precision.
the second is the number of digits to the right of the decimal character. This is called the scale.
For instance, to store a percentage with 1 decimal place (such as the 25,6 you give as an example), you would use the following
decimal(4,1)
That means that you have a maximum of 4 digits, with only 1 of those digits being after the decimal place. It would have a maximum storage of 999.9