xslt format-number decimal, number less than one - xslt

I have this as input called $material_price:
2.40
1000
0.60
They run through
<!-- setup currency rendering -->
<xsl:decimal-format name="dkk" decimal-separator="," grouping-separator="."/>
<xsl:value-of select="format-number($material_price, '#.###,00', 'dkk')"/>
Output is:
2,40
1.000,00
,60
How can I make changes to the xslt so last output is 0,60 and not ,60 (without the zero)

Like this:
<xsl:value-of select="format-number($material_price, '#.##0,00', 'dkk')"/>
The second parameter (picture string) is described as follows by the documentation.
Required. Specifies the format pattern. Here are some of the characters used in the formatting pattern:
0 (Digit)
(Digit, zero shows as absent)
. (The position of the decimal point Example: ###.##)
, (The group separator for thousands. Example: ###,###.##)
% (Displays the number as a percentage. Example: ##%)
; (Pattern separator. The first pattern will be used for positive numbers and the second for negative numbers)

Related

How to extract numbers with multiple decimal points from text in Google Sheets?

I'm trying to extract a software version (pure numbers and decimals) from a text string in a cell, but because it has multiple decimals places I can't get the full result.
Examples (Input --> Output):
Plugin Version v4.5.2 Available --> 4.5.2
New Plugin v1.15.49 Available --> 1.15.49
So far I'm working with this formula, but it only gives me the first decimal result, it can't handle 2 decimals because these are software version numbers, not real numbers.
=REGEXEXTRACT(A1,"-*\d*\.?\d+")
You can also try
=regexextract(A1; "[0-9.]+")
Try like:
=REGEXEXTRACT(A1;"(-*\d*[\.?\d+]+)")
Explanation:
The original:
-*\d*\.?\d+
matches:
-*: 0 to n - characters followed by:
\d*: 0 to n decimal characters (0-9), followed by:
\.?: 0 to 1 . character(s) (it has to be escaped, otherwise it means "any character"), followed by:
\d+: 1 to n decimal characters.
We now:
wrap \.?\d+ into a "selection" ([...])
and match 1 to n(+) of its occurences: [\.?\d+]+
additionally(not mandatory) enclose all in a "capturing group" ((...)) ...we could also: extract parts of it.
sample sheet
https://www.google.com/search?q=regex+tutorial
If you just want the number after v,
=REGEXEXTRACT(A1,"v([\d\.]+)")
\d any digit
\. literal .
[]+ match one or more of any of the characters inside []

regular expression that accepts numbers like 1,000.10? [duplicate]

I need regex to validate a number that could contain thousand separators or decimals using javascript.
Max value being 9,999,999.99
Min value 0.01
Other valid values:
11,111
11.1
1,111.11
INVALID values:
1111
1111,11
,111
111,
I've searched all over with no joy.
/^\d{1,3}(,\d{3})*(\.\d+)?$/
About the minimum and maximum values... Well, I wouldn't do it with a regex, but you can add lookaheads at the beginning:
/^(?!0+\.00)(?=.{1,9}(\.|$))\d{1,3}(,\d{3})*(\.\d+)?$/
Note: this allows 0,999.00, so you may want to change it to:
/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/
which would not allow a leading 0.
Edit:
Tests: http://jsfiddle.net/pKsYq/2/
((\d){1,3})+([,][\d]{3})*([.](\d)*)?
It worked on a few, but I'm still learning regex as well.
The logic should be 1-3 digits 0-1 times, 1 comma followed by 3 digits any number of times, and a single . followed by any number of digits 0-1 times
First, I want to point out that if you own the form the data is coming from, the best way to restrict the input is to use the proper form elements (aka, number field)
<input type="number" name="size" min="0.01" max="9,999,999.99" step="0.01">
Whether "," can be entered will be based on the browser, but the browser will always give you the value as an actual number. (Remember that all form data must be validated/sanitized server side as well. Never trust the client)
Second, I'd like to expand on the other answers to a more robust (platform independent)/modifiable regex.
You should surround the regex with ^ and $ to make sure you are matching against the whole number, not just a subset of it. ex ^<my_regex>$
The right side of the decimal is optional, so we can put it in an optional group (<regex>)?
Matching a literal period and than any chain of numbers is simply \.\d+
If you want to insist the last number after the decimal isn't a 0, you can use [1-9] for "a non-zero number" so \.\d+[1-9]
For the left side of the decimal, the leading number will be non-zero, or the number is zero. So ([1-9]<rest-of-number-regex>|0)
The first group of numbers will be 1-3 digits so [1-9]\d{0,2}
After that, we have to add digits in 3s so (,\d{3})*
Remember ? means optional, so to make the , optional is just (,?\d{3})*
Putting it all together
^([1-9]\d{0,2}(,?\d{3})*|0)(\.\d+[1-9])?$
Tezra's formula fails for '1.' or '1.0'. For my purposes, I allow leading and trailing zeros, as well as a leading + or - sign, like so:
^[-+]?((\d{1,3}(,\d{3})*)|(\d*))(\.|\.\d*)?$
In a recent project we needed to alter this version in order to meet international requirements.
This is what we used: ^-?(\d{1,3}(?<tt>\.|\,| ))((\d{3}\k<tt>)*(\d{3}(?!\k<tt>)[\.|\,]))?\d*$
Creating a named group (?<tt>\.|\,| ) allowed us to use the negative look ahead (?!\k<tt>)[\.|\,]) later to ensure the thousands separator and the decimal point are in fact different.
I have used below regrex for following retrictions -
^(?!0|\.00)[0-9]+(,\d{3})*(.[0-9]{0,2})$
Not allow 0 and .00.
','(thousand seperator) after 3 digits.
'.' (decimal upto 2 decimal places).

Optimization of Regular Expression to match numbers bigger or equal to 50

I want to check if a number is 50 or more using a regular expression. This in itself is no problem but the number field has another regex checking the format of the entered number.
The number will be in the continental format: 123.456,78 (a dot between groups of three digits and always a comma with 2 digits at the end)
Examples:
100.000,00
50.000,00
50,00
34,34
etc.
I want to capture numbers which are 50 or more. So from the four examples above the first three should be matched.
I've come up with this rather complicated one and am wondering if there is an easier way to do this.
^(\d{1,3}[.]|[5-9][0-9]|\d{3}|[.]\d{1,3})*[,]\d{2}$
EDIT
I want to match continental numbers here. The numbers have this format due to internal regulations and specify a price.
Example: 1000 EUR would be written as 1.000,00 EUR
50000 as 50.000,00 and so on.
It's a matter of taste, obviously, but using a negative lookahead gives a simple solution.
^(?!([1-4]?\d),)[1-9](\d{1,2})?(\.\d{3})*,\d{2}\b
In words: starting from a boundary ignore all numbers that start with 1 digit OR 2 digits (the first being a 1,2,3 or 4), followed by a comma.
Check on regex101.com
Try:
EDIT ^(.{3,}|[5-9]\d),\d{2}$
It checks if:
there 3 chars or more before the ,
there are 2 numbers before the , and the first is between 5 and 9
and then a , and 2 numbers
Donno if it answer your question as it'll return true for:
aa50,00
1sdf,54
But this assumes that your original string is a number in the format you expect (as it was not a requirement in your question).
EDIT 3
The regex below tests if the number is valid referring to the continental format and if it's equal or greater than 50. See tests here.
Regex: ^((([1-9]\d{0,2}\.)(\d{3}\.){0,}\d{3})|([1-9]\d{2})|([5-9]\d)),\d{2}$
Explanation (d is a number):
([1-9]\d{0,2}\.): either d., dd. or ddd. one time with the first d between 1 and 9.
(\d{3}\.){0,}: ddd. zero or x time
\d{3}: ddd 3 digit
These 3 parts combined match any numbers equals or greater than 1000 like: 1.000, 22.002 or 100.000.000.
([1-9]\d{2}): any number between 100 and 999.
([5-9]\d)): a number between 5 and 9 followed by a number. Matches anything between 50 and 99.
So it's either the one of the parts above or this one.
Then ,\d{2}$ matches the comma and the two last digits.
I have named all inner groups, for better understanding what part of number is matched by each group. After you understand how it works, change all ?P<..> to ?:.
This one is for any dec number in the continental format.
^(?P<common_int>(?P<int>(?P<int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<int_end>\.\d{3})*|0)(?!,)|(?P<dec_int_having_frac>(?P<dec_int>(?P<dec_int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<dec_int_end>\.\d{3})*,)|0,|,)(?=\d))(?P<frac_from_comma>(?<=,)(?P<frac>(?P<frac_start>\d{3}\.)*(?P<frac_end>\d{1,3})))?$
test
This one is for the same with the limit number>=50
^(?P<common_int>(?P<int>(?P<int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<int_end>\.\d{3})+|(?P<int_short>[1-9]\d{2}|[5-9]\d))(?!,)|(?P<dec_int_having_frac>(?P<dec_int>(?P<dec_int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<dec_int_end>\.\d{3})+,)|(?P<dec_short_int>[1-9]\d{2}|[5-9]\d),)(?=\d))(?P<frac_from_comma>(?<=,)(?P<frac>(?P<frac_start>\d{3}\.)*(?P<frac_end>\d{1,3})))?$
tests
If you always have the integer part under 999.999 and fractal part always 2 digits, it will be a bit more simple:
^(?P<dec_int_having_frac>(?P<dec_int>(?P<dec_int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<dec_int_end>\.\d{3})?,)|(?P<dec_short_int>[1-9]\d{2}|[5-9]\d),)(?=\d)(?P<frac_from_comma>(?<=,)(?P<frac>(?P<frac_end>\d{1,2})))?$
test
If you can guarantee that the number is correctly formed -- that is, that the regex isn't expected to detect that 5,0.1 is invalid, then there are a limited number of passing cases:
ends with \d{3}
ends with [5-9]\d
contains \d{3},
contains [5-9]\d,
It's not actually necessary to do anything with \.
The easiest regex is to code for each of these individually:
(\d{3}$|[5-9]\d$|\d{3},|[5-9]\d)
You could make it more compact and efficient by merging some of the cases:
(\d{3}[$,]|[5-9]\d[$,])
If you need to also validate the format, you will need extra complexity. I would advise against attempting to do both in a single regex.
However unless you have a very good reason for having to do this with a regex, I recommend against it. Parse the string into an integer, and compare it with 50.

Add leading zeros to the negative number in XSLT 1.0

how to format below numbers in XSLT 1.0.The input is some times positive number and some times negative number.
input:
-4
-1
2
output:
00000-4
00000-1
0000002
XSLT provides the format-number function which may help you here.
format-number(theNumber, '0000000')
would give you 0000002 for 2 and -0000004 for -4. But if you really do want to put the leading zeros before the minus sign in the negative case then the easiest approach is to simply treat the whole thing as a string manipulation rather than number formatting. Define a variable
<xsl:variable name="zeros" select="'0000000'" />
and then say something like
concat(substring($zeros, string-length(theNumber)+1), theNumber)
The substring bit works out how many leading zeros to add, e.g. for -4 it will take the substring of $zeros starting from the third character, i.e. five zeros.

JSF telephone numbers regular expression

I'm using primefaces of JSF, I'm doing a regular expression checking for the input of phone number, accepted values are:
909888
+90877845
here's my code
<p:inputText validatorMessage="invalid value">
<f:validateRegex pattern="^[+]?[0-9]+$" />
</p:inputText>
My problem is that the expression does not pass the empty values, as the field is not required
You are really close, you need to surround the entire match in a ()?. This means the regex will need to match the entire inside 1 or 0 times. A blank string will validate against the 0, and your +#### will validate on the time where that pattern is matched once.
^([+]?[\d]+)?$ should work, it also makes it a little easier to read since \d means any digit, but 0-9 should work fine. I did some testing with your values and the new addition here.
Also, if there is a specific count of numbers that can be in range (i.e. valid if the number is between 6 and 8 in length) you can modify the [\d]+ to [\d]{x,y} where x and y are your minimum/maximum length requirements.