Extra leading zeros when printing float using printf? - c++

I'd like to be able to write a time string that looks like this: 1:04:02.1 hours using printf.
When I try to write something like this:
printf("%d:%02d:%02.1f hours\n", 1, 4, 2.123456);
I get:
1:04:2.1 hours
Is it possible to add leading zeros to a float formatting?

With the %f format specifier, the "2" is treated as the minimum number of characters altogether, not the number of digits before the decimal dot. Thus you have to replace it with 4 to get two leading digits + the decimal point + one decimal digit.
printf("%d:%02d:%04.1f hours\n", 1, 4, 2.123456);

Try %04.1f instead of %02.1f. The "4" here means at least 4 characters will be printed, and "2.1" has 3 (> 2) characters, so to enable the padding zeros you need 4.

Related

Repeating sequence with leading zeros to make all values same length in Google Sheets

The following code gives a repeating sequence of specified numbers from 1 to 12, repeating it 12 times. However, the numbers generated have a different length (1,2,3...10,11,12). How can the formula be modified so that leading zeros are added in order to make all numbers the same length?
transpose(split(REPT(concat(JOIN(",",SEQUENCE(1,12)),","),ROUNDDOWN(ROWS(A1:A)/15)),",",true))
For example, in this example there are numbers with length = 2 and then the desired sequence would be 01,02...10,11,12. However if the sequence was up to 3 or more digits, for example:
...(",",SEQUENCE(1,150)),",")...
Then a desired sequence would be 001...010...150. A sequence going up to 4 would be 0001...1500 etc.
try:
=INDEX(TEXT(FLATTEN(MAKEARRAY(25, 12, LAMBDA(x, y, y))), "00"))
BASE formula has min_length argument. You can use it to set the LENgth of your sequences. It's also easy to create the sequence without TRANSPOSE/SPLIT/JOIN/SPLIT with just IF/FLATTEN. For eg, To create sequence of 12, 25 times,
=ARRAYFORMULA(FLATTEN(IF(SEQUENCE(25),BASE(SEQUENCE(1,12),10,LEN(12)))))

Conditional deleting of first digit in SAS variable

I have a character variable called "animid" with values like these:
215298
275899
287796
214896
98154
97856
78-21
213755
21-45
31-457
I want to remove the first digit ("2") only in those numbers that have a length of 6 digits (e.g. 213755, 214896, etc.). I cannot delete the first digit of numbers with a length of 5 or less (e.g. 21-45, 98154).
I used the following code trying to subtract the last 5 digits
data new;
set old;
new_animid =substr (animid,max(1,length(animid)-4),5);
run;
This code effectively keep the last 5 digits for each value. However, it also converts numbers like 31-457 to 1-457 (which is a result that I don't want. I only want to delete the number first digit ONLY if the value has 6 digits AND it starts with "2").
I basically ask if there is a way to state conditions to the "substr" statement (or other method in SAS). Something that will allow me to delete the number "2" but ONLY in those numbers that effectively start with the digit "2" AND that have 6 digits.
To remove the first digit just use SUBSTR(,2).
new_animid = animid ;
if animid =: '2' and length(animid)=6 then new_animid = substr(animid,2);
Use regular expression:
_animid=prxchange('s/^2(\d{5})/$1/',-1,animid);

Regexp for digit with decimal with maximum value

I have a regexp to check for a decimal with 2 numbers, but I want to check both the integer and the decimal part for their length.
/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/;
The above code is decimal with length 2 (ex: 12.23) but I want 10 integer value and 2 decimal value (10,2) like,
1234567890.12
Use /^(?![.])\d{0,10}(\.\d{1,2})?$/
It allows 1.23, 1.2 0.2
Invalid values ., 1.
Depending on what you exactly want, you can use:
/^\s*-?(\d{1,10}(\.\d{1,2})?)\s*$/
for input like: 12.23, 3.4, 1234567890.34, 4, 456, etc., or:
/^\s*-?(\d{10}(\.\d{1,2})?)\s*$/
for: 9087654321, 1234567890.1, 1234567890.23 (10 digits, and optional point and one or two digits), or:
/^\s*-?(\d{10}\.\d{2})\s*$/
for exactly 10 digits fallowed by point and 2 digits, like: 9087654321.12, etc. Its all depends on what kind of numbers you want to filter.

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.

Regex - Validation of numeric with up to 4 decimal places

I am having a bit of difficulty with the following:
I need to allow any positive numeric value up to four decimal places. Here are some examples.
Allowed:
123
12345.4
1212.56
8778787.567
123.5678
Not allowed:
-1
12.12345
-12.1234
I have tried the following:
^[0-9]{0,2}(\.[0-9]{1,4})?$|^(100)(\.[0]{1,4})?$
However this doesn't seem to work, e.g. 1000 is not allowed when it should be.
Any ideas would be greatly appreciated.
Thanks
To explain why your attempt is not working for a value of 1000, I'll break down the expression a little:
^[0-9]{0,2} # Match 0, 1, or 2 digits (can start with a zero)...
(\.[0-9]{1,4})?$ # ... optionally followed by (a decimal, then 1-4 digits)
| # -OR-
^(100) # Capture 100...
(\.[0]{1,4})?$ # ... optionally followed by (a decimal, then 1-4 ZEROS)
There is no room for 4 digits of any sort, much less 1000 (theres only room for a 0-2 digit number or the number 100)
^\d* # Match any number of digits (can start with a zero)
(\.\d{1,4})?$ # ...optionally followed by (a decimal and 1-4 digits)
This expression will pass any of the allowed examples and reject all of the Not Allowed examples as well, because you (and I) use the beginning-of-string assertion ^.
It will also pass these numbers:
.2378
1234567890
12374610237856987612364017826350947816290385
000000000000000000000.0
0
... as well as a completely blank line - which might or might not be desired
to make it reject something that starts with a zero, use this:
^(?!0\d)\d* # Match any number of digits (cannot "START" with a zero)
(\.\d{1,4})?$ # ...optionally followed by (a decimal and 1-4 digits)
This expression (which uses a negative lookahead) has these evaluations:
REJECTED Allowed
--------- -------
0000.1234 0.1234
0000 0
010 0.0
You could also test for a completely blank line in other ways, but if you wanted to reject it with the regex, use this:
^(?!0\d|$)\d*(\.\d{1,4})?$
Try this:
^[0-9]*(?:\.[0-9]{0,4})?$
Explanation: match only if starting with a digit (excluding negative numbers), optionally followed by (non-capturing group) a dot and 0-4 digits.
Edit: With this pattern .2134 would also be matched. To only allow 0 < x < 1 of format 0.2134, replace the first * with a + above.
This regex would do the trick:
^\d+(?:\.\d{1,4})?$
From the beginning of the string search for one or more digits. If there's a . it must be followed with atleast one digit but a maximum of 4.
^(?<!-)\+?\d+(\.?\d{0,4})?$
The will match something with doesn't start with -, maybe has a + followed by an integer part with at least one number and an optional floating part of maximum 4 numbers.
Note: Regex does not support scientific notation. If you want that too let me know in a comment.
Well asked!!
You can try this:
^([0-9]+[\.]?[0-9]?[0-9]?[0-9]?[0-9]?|[0-9]+)$
If you have a double value but it goes to more decimal format and you want to shorter it to 4 then !
double value = 12.3457652133
value =Double.parseDouble(new DecimalFormat("##.####").format(value));